This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Quick sort algorithm in C++ . * Radix sort algorithm in C++ added. * some description comments added * spelling errors in comments corrected * Restyled by astyle * Restyled by clang-format Co-authored-by: Roshan Prakash Raut <[email protected]> Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
1 parent
f9a5b9c
commit 87278e3
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
QUICK SORT ALGORITHM : TIME COMPLEXITY O(nlogn(n)) | ||
*/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// note: call by address in swap function | ||
void swap(int *a, int *b) { | ||
int t; | ||
t = *a; | ||
*a = *b; | ||
*b = t; | ||
} | ||
|
||
// partition function which makes partition in array and return the partition | ||
// index | ||
int partition(int a[], int low, int high) { | ||
// i is leftmost index and j is the rightmost index | ||
int i = low, j = high; | ||
int pivot = a[low]; | ||
|
||
do { | ||
do { | ||
i++; | ||
} while ( | ||
a[i] <= | ||
pivot); // note :condition is for continuation of loop ans not for stop | ||
|
||
do { | ||
j--; | ||
} while ( | ||
a[j] > | ||
pivot); // note: condition is for continuation of loop and not for stop | ||
|
||
if (i < j) // note: condition | ||
swap(&a[i], &a[j]); // note: | ||
|
||
} while (i < j); // note:condition | ||
|
||
swap(&a[low], &a[j]); // note: | ||
|
||
return j; | ||
} | ||
|
||
// Quick sort function | ||
void Quick_sort(int a[], int low, int high) { | ||
int j; // j is partition index | ||
|
||
// note: This condition for says that min two elements are required for quick | ||
// sort | ||
if (low < high) { | ||
// index j is partition index | ||
j = partition(a, low, high); | ||
|
||
// left partition // note: low to j is for left partition | ||
Quick_sort(a, low, j); | ||
|
||
// right partition //note: j+1 to high is for right partition | ||
Quick_sort(a, j + 1, high); | ||
} | ||
} | ||
|
||
// main function | ||
int main() { | ||
|
||
int arr[] = {2, 4, 3, 6, 9, 8, -1}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
|
||
cout << "Before Quick Sort array is : "; | ||
for (int i = 0; i < n; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
// Quick sort | ||
Quick_sort(arr, 0, n); | ||
|
||
cout << "After Quick Sort array is : "; | ||
for (int i = 0; i < n; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
cout << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
RADIX SORT ALGORITHM : TIME COMPLEXITY : O(nlog(n)) | ||
*/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// function to get the max value from array elements | ||
int getMax(int arr[], int n) { | ||
// mx stores the max elements of all the array elements | ||
int mx = arr[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
|
||
if (arr[i] > mx) | ||
mx = arr[i]; | ||
} | ||
|
||
return mx; | ||
} | ||
|
||
// print function to print the array | ||
void print(int arr[], int n) { | ||
for (int i = 0; i < n; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
} | ||
|
||
// countSort Implementing | ||
void countSort(int arr[], int n, int exp) { | ||
// output array stores the required sorted array | ||
int output[n]; | ||
int i, count[10] = {0}; | ||
|
||
// Storing the count of occurrences of each element digits in count[] array | ||
for (i = 0; i < n; i++) { | ||
// here counting digits if each elements | ||
count[(arr[i] / exp) % 10]++; | ||
} | ||
|
||
// Change count[i] so that count[i] now contains actual | ||
// position of this digit in output[] | ||
for (i = 1; i < 10; i++) | ||
count[i] += count[i - 1]; | ||
|
||
// output array building | ||
for (i = n - 1; i >= 0; i--) { | ||
|
||
// here counting each digits of the array elements | ||
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | ||
count[(arr[i] / exp) % 10]--; | ||
} | ||
|
||
// array now contains sorted numbers according to current digit | ||
for (i = 0; i < n; i++) { | ||
arr[i] = output[i]; | ||
} | ||
} | ||
|
||
// Raix sort function | ||
void radix_sort(int arr[], int n) { | ||
// here m stores the maximum element of the array | ||
int m = getMax(arr, n); | ||
|
||
// if m = 3 then exp values are 1 so elements are sorted on basis of unit | ||
// place digits if m = 32 then exp values are 1, 10 so elements are sorted on | ||
// basis of unit and tens place digits if m = 329 then exp values are 1, 10, | ||
// 100 so elements are sorted on basis of unit, tens and hundreds place digits | ||
// if m = 3299 then exp values are 1, 10, 100, 1000 so elments are sorted on | ||
// basis of unit, tens , hundreds and thousands place digits | ||
|
||
for (int exp = 1; m / exp > 0; exp *= 10) | ||
countSort(arr, n, exp); | ||
} | ||
|
||
// main function | ||
int main() { | ||
// N - number of elements | ||
int N; | ||
|
||
// input array size | ||
cin >> N; | ||
|
||
int arr[N]; | ||
|
||
for (int i = 0; i < N; i++) { | ||
// input array elements | ||
cin >> arr[i]; | ||
} | ||
|
||
// calling radix sort function | ||
radix_sort(arr, N); | ||
|
||
// printing the sorted array | ||
cout << "The sorted array is : "; | ||
|
||
print(arr, N); | ||
} |