Created
April 1, 2019 10:59
-
-
Save jatinsharrma/7328ce44a1831dfdfa9480f3814e3ebc to your computer and use it in GitHub Desktop.
Selection Sort using pointers - call by reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int swap(int* i, int* j){ | |
| int temp = *i; | |
| *i = *j; | |
| *j = temp; | |
| } | |
| int SelectionSort(int array[], int size){ | |
| for (int i =0 ; i < size ; i++){ | |
| int index = i; | |
| for (int j = i ; j<size ; j++){ | |
| if (array[j] < array[index]){ | |
| index = j; | |
| } | |
| } | |
| swap(&array[i],&array[index]); | |
| } | |
| } | |
| void print(int array[], int size){ | |
| for (int i = 0 ; i<size ; i++){ | |
| printf("%d\n",array[i]); | |
| } | |
| } | |
| void main() { | |
| int array[] = {6,8,7,6,5,9,3,2,1,0}; | |
| int size = sizeof(array)/sizeof(array[0]); | |
| SelectionSort(array,size); | |
| print(array,size); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment