#include<bits/stdc++.h> #define swap(x,y) { x = x + y; y = x - y; x = x - y; } using namespace std; void bubbleSort(int arr[], int n) { int i, j; bool flag; // Outer pass for(i = 0; i < n; i++) { flag = false; // Set flag as false for(j = 0; j < n-i-1; j++) { // Compare values if( arr[j] > arr[j+1]) { swap(arr[j],arr[j+1]); flag = true; } } if(!flag) { break; } } } int main(int argv, char* argc[]) { int arr[] = {1,5,6,8,3,4,7,2,9}; int n = sizeof(arr)/sizeof(int); cout<<"Unsorted Array :"; for(int i=0;i<n;i++) // Print the Original Array cout<<arr[i]<<" "; cout<<endl; bubbleSort(arr,n); // Call for Bubble Sort Function cout<<"Sorted Array :"; for(int i=0;i<n;i++) // Print the Sorted Array cout<<arr[i]<<" "; return(0); }
The function bubbleSort takes two arguments and n, Sort the array arr[0...n-1] using Bubble Sort.