added quick sort (#48)
* add binary search in js * added merge sort in c * added quick sort in cpp * modified quick sortpull/54/head
parent
a27fdb30dd
commit
f84976ab83
|
@ -6,6 +6,7 @@
|
|||
2. [Insertion Sort](c-or-cpp/insertion-sort.cpp)
|
||||
3. [Selection Sort](c-or-cpp/selection-sort.cpp)
|
||||
4. [Merge Sort](c-or-cpp/merge-sort.c)
|
||||
5. [Quick Sort](c-or-cpp/quick-sort.cpp)
|
||||
|
||||
### C#
|
||||
|
||||
|
|
|
@ -13,11 +13,11 @@ int main()
|
|||
printf("Enter the elements of the array:\n");
|
||||
for (int i = 0; i < n; i++)
|
||||
scanf("%d", &a[i]);
|
||||
printf( "Given array is: ");
|
||||
printf("Given array is: ");
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("%d ", a[i]);
|
||||
mergeSort(a, 0, n - 1);
|
||||
printf( "\nSorted array is: ");
|
||||
printf("\nSorted array is: ");
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("%d ", a[i]);
|
||||
return 0;
|
||||
|
@ -73,7 +73,6 @@ void merging(int a[], int l, int m, int r)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// l is for left index and r is
|
||||
// right index of the sub-array
|
||||
// of arr to be sorted
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
int partition(int a[],int low,int high);
|
||||
void quickSort(int a[], int low, int high);
|
||||
|
||||
// Driver Code
|
||||
int main()
|
||||
{
|
||||
cout<<"Enter the length of array"<<endl;
|
||||
int n;
|
||||
cin>>n;
|
||||
int *a = new int(n);
|
||||
// Getting elements of array
|
||||
cout<<"Enter the elements of array"<<endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
cin>>a[i];
|
||||
cout << "Original array: \n";
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << a[i] << " ";
|
||||
quickSort(a, 0, n - 1);
|
||||
cout << "\nSorted array: \n";
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << a[i] << " ";
|
||||
delete (a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A utility function to swap two elements
|
||||
void swap(int *a, int *b)
|
||||
{
|
||||
int t = *a;
|
||||
*a = *b;
|
||||
*b = t;
|
||||
}
|
||||
|
||||
/* This function takes last element as pivot, places
|
||||
the pivot element at its correct position in sorted
|
||||
aay, and places all smaller (smaller than pivot)
|
||||
to left of pivot and all greater elements to right
|
||||
of pivot */
|
||||
int partition(int a[], int low, int high)
|
||||
{
|
||||
int pivot = a[high];
|
||||
int i = (low - 1);
|
||||
|
||||
for (int j = low; j <= high - 1; j++)
|
||||
{
|
||||
// If current element is smaller than the pivot
|
||||
if (a[j] < pivot)
|
||||
{
|
||||
i++; // increment index of smaller element
|
||||
swap(&a[i], &a[j]);
|
||||
}
|
||||
}
|
||||
swap(&a[i + 1], &a[high]);
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
/* The main function that implements QuickSort
|
||||
a[] --> array to be sorted,
|
||||
low --> Starting index,
|
||||
high --> Ending index */
|
||||
void quickSort(int a[], int low, int high)
|
||||
{
|
||||
if (low < high)
|
||||
{
|
||||
/* pi is partitioning index, a[p] is now
|
||||
at right place */
|
||||
int pi = partition(a, low, high);
|
||||
|
||||
// Separately sort elements before
|
||||
// partition and after partition
|
||||
quickSort(a, low, pi - 1);
|
||||
quickSort(a, pi + 1, high);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
# include <iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
|
|
Loading…
Reference in New Issue