waltermilner

Untitled

Apr 30th, 2021
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. #define SIZE 10
  6. int data[SIZE]={6,1,9,3,2,4,8,7,0,5};
  7. int copy[SIZE];
  8. // copy array
  9. for (int i=0; i<SIZE; i++)
  10.   copy[i]=data[i];
  11. // partial bubble sort data - largest at start
  12. // only do SIZE/2 scans..
  13. for (int i=0; i<SIZE/2; i++)
  14.   for (int j=SIZE-1; j>0; j--)
  15.     if (data[j]>data[j-1])
  16.     {
  17.       int temp=data[j];
  18.       data[j]=data[j-1];
  19.       data[j-1]=temp;
  20.     }
  21. int pivot=data[SIZE/2-1]; // last sorted value
  22. int whereToAdd=SIZE/2; // where to place the rest
  23.  
  24. // go through copy of original
  25. for (int i=0; i<SIZE;i++)
  26.   if (copy[i]<=pivot) // if find a smaller one
  27.   {
  28.     data[whereToAdd]=copy[i];// copy to data
  29.     whereToAdd++; // and move on place
  30.   }
  31. // check result
  32. for (int i=0; i<SIZE; i++)
  33.   printf("%d ", data[i]);
  34. printf("\n");
  35. // get 9 8 7 6 5 1 3 2 4 0
  36.  
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment