Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3e38d9665c
Fetching contributors…

Cannot retrieve contributors at this time

file 35 lines (31 sloc) 0.838 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
BubbleSort: Useful for small datasets and already sorted
datasets with small numbers of unsorted elements
*/

void bubblesort(int* array, int arraySize) {
  int i = 0;
  for (; i < arraySize; i++) {
    int j = i + 1;
    for (; j < arraySize; j++) {
      // if the this element is smaller than the next, switch them
      int item = array[i];
      int nextItem = array[j];
      if (item > nextItem) {
        int temp = array[j];
        array[j] = array[i];
        array[i] = temp;
      }
    }
  }
}

int main() {
  const int NUMBERS_SIZE = 21;
  int numbers[] = {9, 6, 5, 4, 3, 2, 1, 4, 9, 10, 100, 67, 43, 213, 1, 23, 5, 3, 0, 12, -1};
  
  bubblesort(numbers, NUMBERS_SIZE);
  
  int i = 0;
  for (; i < NUMBERS_SIZE; i++) {
    printf("%d", numbers[i]);
    if (i + 1 < NUMBERS_SIZE) { printf(","); }
  }
  return 0;
}
Something went wrong with that request. Please try again.