0% found this document useful (0 votes)
39 views1 page

Sorting Code

This document defines an insertion sort function that sorts an array of integers in ascending order. It takes in an integer array and size as parameters. It uses a nested for loop to iterate through the array, comparing each element to its predecessor and swapping if out of order. The main function calls insertion sort on a sample array, prints the sorted output, and includes a function prototype for selection sort but does not implement it.

Uploaded by

alabbass
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views1 page

Sorting Code

This document defines an insertion sort function that sorts an array of integers in ascending order. It takes in an integer array and size as parameters. It uses a nested for loop to iterate through the array, comparing each element to its predecessor and swapping if out of order. The main function calls insertion sort on a sample array, prints the sorted output, and includes a function prototype for selection sort but does not implement it.

Uploaded by

alabbass
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>;

using namespace std;

void InsertionSort( int num[], int size) { int i, j, key; for(j = 1; j < size; j++) // Start with 1 (not 0) { key = num[j]; for(i = j - 1; (i >= 0) && (num[i] > key); i--) // larger values move up { num[i+1] = num[i]; } num[i+1] = key; //Put key into its proper location } return; } void selectionSort(int num[], int size); int main(){ int const sze = 4; int grades[sze]= {2, 1, 4,3}; InsertionSort(grades,sze); for (int i = 0 ;i <sze ; i++) cout << grades[i]<<" ";

return 0; } void selectionSort(int num[], int size){ int exPos; for (int i = 0 ; i <size; i ++){ exPos = i; for (int j = i+1;j<size;j++ ) if(num[j]<num[exPos]) exPos = j; if (exPos!=i) swap(num[exPos],num[i]); } }

You might also like