-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2170 from aryanwani/master
Sorting Algorithms
- Loading branch information
Showing
3 changed files
with
259 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,88 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
int cnt; | ||
void heapify(int arr[], int n, int i) | ||
{ | ||
int largest = i; | ||
int l = 2 * i + 1; | ||
int r = 2 * i + 2; | ||
if (l < n && arr[l] > arr[largest]) | ||
{ | ||
|
||
largest = l; | ||
} | ||
|
||
if (r < n && arr[r] > arr[largest]) | ||
{ | ||
|
||
largest = r; | ||
} | ||
cnt++; | ||
if (largest != i) | ||
{ | ||
|
||
swap(arr[i], arr[largest]); | ||
heapify(arr, n, largest); | ||
} | ||
} | ||
void buildheap(int arr[], int n) | ||
{ | ||
for (int i = n / 2 - 1; i >= 0; i--) | ||
{ | ||
|
||
heapify(arr, n, i); | ||
} | ||
} | ||
void heapSort(int arr[], int n) | ||
{ | ||
buildheap(arr, n); | ||
|
||
for (int i = n - 1; i > 0; i--) | ||
{ | ||
cnt++; | ||
swap(arr[0], arr[i]); | ||
heapify(arr, i, 0); | ||
} | ||
} | ||
int main() | ||
{ | ||
int n, c1, c2, c3; | ||
cout << "Enter the no of elements to be sorted" << endl; | ||
cin >> n; | ||
int a[1000], b[1000], c[1000]; | ||
cout << "Enter the elements" << endl; | ||
for (int i = 0; i < n; i++) | ||
cin >> a[i]; | ||
cnt = 0; | ||
heapSort(a, n); | ||
cout << "Elements after sorting" << endl; | ||
for (int i = 0; i < n; i++) | ||
cout << a[i] << " "; | ||
cout << endl; | ||
cout << "No of count is " << cnt << endl; | ||
cout << "SIZE"<< " " << "ASC"<< " " << "DESC" << " " << "RAND" << endl; | ||
for (int i = 8; i < 1000; i = i * 2) | ||
{ | ||
int ln = 2 * i * log(i) / log(2); | ||
for (int j = 0; j < i; j++) | ||
{ | ||
a[j] = j; | ||
b[j] = i - j; | ||
c[j] = rand() % i; | ||
} | ||
cnt = 0; | ||
heapSort(a, i); | ||
c1 = cnt; | ||
cnt = 0; | ||
heapSort(b, i); | ||
c2 = cnt; | ||
cnt = 0; | ||
heapSort(c, i); | ||
c3 = cnt; | ||
cout << endl; | ||
cout << i << " " << c1 << " " << ln << " " << c2 << " " << ln << " " << c3 << " " << ln << endl; | ||
} | ||
cout << endl; | ||
|
||
return (0); | ||
} |
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,88 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
int cnt = 0; | ||
void merge(int arr[], int l, int m, int h) | ||
{ | ||
int n1 = m - l + 1, n2 = h - m; | ||
int left[n1], right[n2]; | ||
for (int i = 0; i < n1; i++) | ||
left[i] = arr[i + l]; | ||
for (int j = 0; j < n2; j++) | ||
right[j] = arr[m + 1 + j]; | ||
int i = 0, j = 0, k = l; | ||
while (i < n1 && j < n2) | ||
{ | ||
if (left[i] <= right[j]) | ||
{ | ||
arr[k++] = left[i++]; | ||
cnt++; | ||
} | ||
else | ||
{ | ||
arr[k++] = right[j++]; | ||
cnt++; | ||
} | ||
} | ||
while (i < n1) | ||
{ | ||
arr[k++] = left[i++]; | ||
cnt++; | ||
} | ||
while (j < n2) | ||
{ | ||
arr[k++] = right[j++]; | ||
cnt++; | ||
} | ||
} | ||
|
||
void mergeSort(int arr[], int l, int r) | ||
{ | ||
if (r > l) | ||
{ | ||
int m = l + (r - l) / 2; | ||
mergeSort(arr, l, m); | ||
mergeSort(arr, m + 1, r); | ||
merge(arr, l, m, r); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n, c1, c2, c3; | ||
cout << "Enter the no of elements to be sorted" << endl; | ||
cin >> n; | ||
int a[1000], b[1000], c[1000]; | ||
cout << "Enter the elements" << endl; | ||
for (int i = 0; i < n; i++) | ||
cin >> a[i]; | ||
cnt = 0; | ||
mergeSort(a, 0, n - 1); | ||
cout << "Elements after sorting" << endl; | ||
for (int i = 0; i < n; i++) | ||
cout << a[i] << " "; | ||
cout << endl; | ||
cout << "No of count is " << cnt << endl; | ||
printf("SIZE\tASC\ttime\tDESC\ttime\tRAND\ttime\n"); | ||
for (int i = 8; i < 1000; i = i * 2) | ||
{ int ln=2*i*log(i)/log(2); | ||
for (int j = 0; j < i; j++) | ||
{ | ||
a[j] = j; | ||
b[j] = i - j; | ||
c[j] = rand() % i; | ||
} | ||
cnt = 0; | ||
mergeSort(a, 0, i - 1); | ||
c1 = cnt; | ||
cnt = 0; | ||
mergeSort(b, 0, i - 1); | ||
c2 = cnt; | ||
cnt = 0; | ||
mergeSort(c, 0, i - 1); | ||
c3 = cnt; | ||
cout << endl; | ||
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i,c1,ln,c2,ln,c3,ln); | ||
} | ||
cout << endl; | ||
return (0); | ||
} |
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,83 @@ | ||
#include<iostream> | ||
#include<cmath> | ||
using namespace std; | ||
int cnt; | ||
int iPartition(int a[], int left, int right) | ||
{ | ||
int i, j, pivot, temp; | ||
pivot = a[left]; | ||
i = left + 1; | ||
j = right; | ||
while (1) | ||
{ | ||
while (pivot >= a[i] && i <= right) | ||
{ | ||
i++; | ||
cnt++; | ||
} | ||
while (pivot < a[j] && j > left) | ||
{ | ||
j--; | ||
cnt++; | ||
} | ||
if (i < j) | ||
{ | ||
swap(a[i], a[j]); | ||
} | ||
else | ||
{ | ||
a[left] = a[j]; | ||
a[j] = pivot; | ||
return j; | ||
} | ||
} | ||
} | ||
void qSort(int arr[], int l, int h) | ||
{ | ||
if (l < h) | ||
{ | ||
int p = iPartition(arr, l, h); | ||
qSort(arr, l, p - 1); | ||
qSort(arr, p + 1, h); | ||
} | ||
} | ||
int main() | ||
{ | ||
int n, c1, c2, c3; | ||
cout << "Enter the no of elements to be sorted" << endl; | ||
cin >> n; | ||
int a[1000], b[1000], c[1000]; | ||
cout << "Enter the elements" << endl; | ||
for (int i = 0; i < n; i++) | ||
cin >> a[i]; | ||
cnt = 0; | ||
qSort(a, 0, n - 1); | ||
cout << "Elements after sorting" << endl; | ||
for (int i = 0; i < n; i++) | ||
cout << a[i] << " "; | ||
cout << endl; | ||
cout << "No of count is " << cnt << endl; | ||
printf("SIZE\tASC\ttime\tDESC\ttime\tRAND\ttime\n"); | ||
for (int i = 8; i < 1000; i = i * 2) | ||
{ int ln=2*i*log(i)/log(2); | ||
for (int j = 0; j < i; j++) | ||
{ | ||
a[j] = j; | ||
b[j] = i - j; | ||
c[j] = rand() % i; | ||
} | ||
cnt = 0; | ||
qSort(a, 0, i - 1); | ||
c1 = cnt; | ||
cnt = 0; | ||
qSort(b, 0, i - 1); | ||
c2 = cnt; | ||
cnt = 0; | ||
qSort(c, 0, i - 1); | ||
c3 = cnt; | ||
cout << endl; | ||
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i,c1,i*i,c2,i*i,c3,ln); | ||
} | ||
cout << endl; | ||
return (0); | ||
} |