Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

void bubbleSort(vector<int>& arr) {
   for (int i = 0; i < n-1; i++) {      
       // Last i elements are already in place   
       for (int j = 0; j < n-i-1; j++) {
           if (arr[j] > arr[j+1]) {
              swap(&arr[j], &arr[j+1]);
           }
        }
     }
}

results matching ""

    No results matching ""