Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //function declaration
  4. void selectionSort(int *a, int n);
  5.  
  6. int main(){
  7. //variable declaration
  8. int arr[5], i;
  9.  
  10. //input
  11. for(i = 0; i < 5; i++)
  12. scanf("%d", &arr[i]);
  13.  
  14. //sort
  15. selectionSort(arr, 5); //passing arr address and no. of elements
  16.  
  17. //output
  18. for(i = 0; i < 5; i++)
  19. printf("%d\n", arr[i]);
  20.  
  21. return 0;
  22. }
  23.  
  24. //function definition
  25. void selectionSort(int *a, int n){
  26. int k, j, pos, small, temp;
  27. for(k = 1; k <= n-1; k++){
  28. small = a[k-1];
  29. pos = k-1;
  30. for(j = k; j <= n-1; j++){
  31. if(a[j] < small){
  32. small = a[j];
  33. pos = j;
  34. }
  35. }
  36. if(pos != k-1){
  37. temp = a[k-1];
  38. a[k-1] = a[pos];
  39. a[pos] = temp;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement