Mr_HO1A

Selection Sort

Sep 27th, 2018
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Init Some Global Vars
  4. int size = 10;
  5. int arr[10];
  6. int passCount = 0;
  7. // End
  8.  
  9. // Function to print array
  10. void printArray(){
  11.     int i = 0;
  12.     for(i = 0; i<size; i++)
  13.         {
  14.         printf("%d \t",arr[i]);
  15.     }
  16. }
  17.  
  18. // Function To Selection Sort Array
  19. void selectionSort(){
  20.     int i,j,maxIndex;
  21.     for(i=0;i<size-1;i++){
  22.         maxIndex = i;
  23.         for(j = i+1; j < size; j++){
  24.             if(arr[j] < arr[maxIndex]){
  25.                 maxIndex = j;
  26.             }
  27.         }
  28.         if(maxIndex != i){
  29.             int temp = arr[i];
  30.             arr[i] = arr[maxIndex];
  31.             arr[maxIndex] = temp;
  32.             passCount++;
  33.         }
  34.     }
  35. }
  36.  
  37. int main(){
  38.     int i = 0;
  39.     for(i = 0;i<size;i++)
  40.     {
  41.         printf("Enter Element For Position [ %d ] : ",i+1);
  42.         scanf("%d",&arr[i]);
  43.     }
  44.     printf("Supplied Array Is : \n");
  45.     printArray();
  46.     printf("Array Created Starting Sort \n");
  47.     selectionSort();
  48.     printf("Sorted Array Is : \n");
  49.     printArray();
  50.     printf("Total Number of Passes : %d",passCount);
  51. }
Add Comment
Please, Sign In to add comment