Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void bubble_sort(int values[], int size)
- {
- // if no swaps were made we know the array is sorted
- bool swaps_made;
- // help with swaping
- int tmp;
- // don't make comparisons till the end of the array every time
- // because going through the array once "bubbles" one element to the top
- int compare_till = size - 1;
- do
- {
- swaps_made = false;
- // loop for every elemtn up to the sorted portion
- for (int i = 0; i < compare_till; i++)
- {
- // if the current element is bigger than the next one
- if (values[i] > values[i + 1])
- {
- // swap them
- tmp = values[i];
- values[i] = values[i + 1];
- values[i + 1] = tmp;
- // be sure to go through the array again next time
- swaps_made = true;
- }
- }
- // one element has "bubbled up", so make one less comparisons next time
- compare_till--;
- }
- while (swaps_made);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement