Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. // C program for implementation of selection sort
  2. #include <stdio.h>
  3.  
  4. // Driver program to test above functions
  5. int main()
  6. {
  7. int arr[] = {1,0,1,1,6,2,1,3};
  8. int n = 8;
  9. int i, j, min_idx;
  10.  
  11. // One by one move boundary of unsorted subarray
  12. for (i = 0; i < n-1; i++)
  13. {
  14. for (int g=0; g < n; g++)
  15. printf("%d ", arr[g]);
  16. printf("\n");
  17.  
  18. // Find the minimum element in unsorted array
  19. min_idx = i;
  20. for (j = i+1; j < n; j++)
  21. if (arr[j] < arr[min_idx])
  22. min_idx = j;
  23.  
  24. // Swap the found minimum element with the first element
  25. int temp = arr[min_idx];
  26. arr[min_idx] = arr[i];
  27. arr[i] = temp;
  28. }
  29.  
  30. for (int g=0; g < n; g++)
  31. printf("%d ", arr[g]);
  32. printf("\n");
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement