Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(void)
- {
- #define SIZE 10
- int data[SIZE]={6,1,9,3,2,4,8,7,0,5};
- int copy[SIZE];
- // copy array
- for (int i=0; i<SIZE; i++)
- copy[i]=data[i];
- // partial bubble sort data - largest at start
- // only do SIZE/2 scans..
- for (int i=0; i<SIZE/2; i++)
- for (int j=SIZE-1; j>0; j--)
- if (data[j]>data[j-1])
- {
- int temp=data[j];
- data[j]=data[j-1];
- data[j-1]=temp;
- }
- int pivot=data[SIZE/2-1]; // last sorted value
- int whereToAdd=SIZE/2; // where to place the rest
- // go through copy of original
- for (int i=0; i<SIZE;i++)
- if (copy[i]<=pivot) // if find a smaller one
- {
- data[whereToAdd]=copy[i];// copy to data
- whereToAdd++; // and move on place
- }
- // check result
- for (int i=0; i<SIZE; i++)
- printf("%d ", data[i]);
- printf("\n");
- // get 9 8 7 6 5 1 3 2 4 0
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment