diff --git a/Cpp-QuickSort.cpp b/Cpp-QuickSort.cpp new file mode 100644 index 0000000..80b1271 --- /dev/null +++ b/Cpp-QuickSort.cpp @@ -0,0 +1,58 @@ +// Quick sort algorithm in C++ + +#include +using namespace std; + +// Swapping position of elements +void swapPositions(int *a, int *b) { + int c = *a; + *a = *b; + *b = c; +} + +// Printing elements of an array +void printArrayElements(int array[], int size) { + int i; + for (i = 0; i < size; i++){ + cout << array[i] << " "; + } + cout << endl; +} + +// Partitioning the array +int partitionArray(int array[], int low, int high,int n) { + int pivot = array[high]; //Pivot Element + int i = (low - 1); + + for (int j = low; j < high; j++) { + if (array[j] <= pivot) { + i++; + swapPositions(&array[i], &array[j]); + } + } + printArrayElements(array, n); + cout << "______________________________________________\n"; + swapPositions(&array[i + 1], &array[high]); + return (i + 1); +} + +void quickSort(int array[], int low, int high,int n) { + if (low < high) { + int pi = partitionArray(array, low, high,n); + + // Sorting elements on the left of pivot + quickSort(array, low, pi - 1,n); + + // Sorting elements on the right of pivot + quickSort(array, pi + 1, high,n); + } +} + + +int main() { + int data[] = {9,10,11,4,6,3,7,1,5,0,15}; + int n = sizeof(data) / sizeof(data[0]); + quickSort(data, 0, n - 1,n); + cout << "\nSorted array in ascending order using the Quick Sort Algorithm: \n"; + printArrayElements(data, n); +} diff --git a/bubblesort.cpp b/bubblesort.cpp index b34f27f..37028d4 100644 --- a/bubblesort.cpp +++ b/bubblesort.cpp @@ -1,32 +1,32 @@ -//CPP program for bubblesort of two array -#include - -using namespace std; - -int main() -{ - int a[50],n,i,j,temp; - cout<<"Enter the size of array: "; - cin>>n; - cout<<"Enter the array elements: "; - - for(i=0;i>a[i]; - - for(i=1;ia[j+1]) - { - temp=a[j]; - a[j]=a[j+1]; - a[j+1]=temp; - } - } - - cout<<"Array after bubble sort:"; - for(i=0;i + +using namespace std; + +int main() +{ + int a[50],n,i,j,temp; + cout<<"Enter the size of array: "; + cin>>n; + cout<<"Enter the array elements: "; + + for(i=0;i>a[i]; + + for(i=1;ia[j+1]) + { + temp=a[j]; + a[j]=a[j+1]; + a[j+1]=temp; + } + } + + cout<<"Array after bubble sort:"; + for(i=0;i