Skip to content

Commit

Permalink
Merge pull request #2170 from aryanwani/master
Browse files Browse the repository at this point in the history
Sorting Algorithms
  • Loading branch information
fineanmol authored Oct 3, 2022
2 parents 4008836 + 6bff77c commit 44aa4c9
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 0 deletions.
88 changes: 88 additions & 0 deletions HeapSort.cpp
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);
}
88 changes: 88 additions & 0 deletions MergeSort.cpp
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);
}
83 changes: 83 additions & 0 deletions QuickSort.cpp
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);
}

0 comments on commit 44aa4c9

Please sign in to comment.