forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #502 from Suhas-Kalagotla/organize_files/sorting_p…
…rograms seperated all sorting programs
- Loading branch information
Showing
28 changed files
with
1,601 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,57 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
/* | ||
Best Time Complexity : O(n) | ||
Average Time Complexity : O(n^2) | ||
Worst Time Complexity : O(n^2) | ||
Worst Space Complexity : O(1) | ||
*/ | ||
|
||
//Swap Function | ||
void swap(int *x, int *y){ | ||
int temp; | ||
temp=*x; | ||
*x=*y; | ||
*y=temp; | ||
} | ||
|
||
//Print Function | ||
void printArray(int A[], int n){ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout<<A[i]<<", "; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
//Sort Function | ||
void bubbleSort(int A[], int n){ | ||
|
||
for (int i = 0; i < n-1; i++) | ||
{ | ||
bool flag = true; //Break if already Sorted | ||
for (int j = 0; j <n-1-i ; j++) | ||
{ | ||
if(A[j]>A[j+1]){ | ||
flag = false; | ||
swap(A[j],A[j+1]); // Using Swap Function | ||
} | ||
} | ||
if(flag==true) | ||
break; | ||
} | ||
} | ||
|
||
|
||
|
||
int main(){ | ||
|
||
int A[] = {1, 2, 5, 6, 12, 54, 625, 7, 23, 9, 987}; | ||
|
||
int n = 11; | ||
printArray(A, n); | ||
bubbleSort(A, n); | ||
printArray(A, n); | ||
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,44 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
/* | ||
Best Time Complexity : O(n) | ||
Average Time Complexity : O(n^2) | ||
Worst Time Complexity : O(n^2) | ||
Worst Space Complexity : O(1) | ||
*/ | ||
|
||
void printArray(int A[], int n){ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout<<A[i]<<", "; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
void insertionSort(int A[], int n){ | ||
int key, j; | ||
|
||
for (int i = 1; i <= n-1; i++) | ||
{ | ||
key = A[i]; | ||
j = i-1; | ||
|
||
while(j>=0 && A[j] > key){ | ||
A[j+1] = A[j]; | ||
j--; | ||
} | ||
A[j+1] = key; | ||
} | ||
} | ||
|
||
int main(){ | ||
|
||
|
||
int A[] = {12, 54, 65, 7, 23, 9}; | ||
int n = 6; | ||
printArray(A, n); | ||
insertionSort(A, n); | ||
printArray(A, n); | ||
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,48 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
/* | ||
Best Time Complexity : O(n^2) | ||
Average Time Complexity : O(n^2) | ||
Worst Time Complexity : O(n^2) | ||
Worst Space Complexity : O(1) | ||
*/ | ||
|
||
void printArray(int A[], int n){ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout<<A[i]<<", "; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
void selectionSort(int A[], int n){ | ||
int indexOfMin, temp; | ||
|
||
for (int i = 0; i < n-1; i++) | ||
{ | ||
indexOfMin = i; | ||
for (int j = i+1; j < n; j++) | ||
{ | ||
if(A[j] < A[indexOfMin]){ | ||
indexOfMin = j; | ||
} | ||
} | ||
|
||
temp = A[i]; | ||
A[i] = A[indexOfMin]; | ||
A[indexOfMin] = temp; | ||
} | ||
} | ||
|
||
int main(){ | ||
|
||
|
||
int A[] = {3, 5, 2, 13, 12}; | ||
int n = 5; | ||
printArray(A, n); | ||
selectionSort(A, n); | ||
printArray(A, n); | ||
|
||
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,75 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
/* | ||
Best Time Complexity : O(nlogn) | ||
Average Time Complexity : O(nlogn) | ||
Worst Time Complexity : O(n^2) | ||
Worst Space Complexity : O(logn) | ||
*/ | ||
|
||
void printArray(int A[], int n){ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout<<A[i]<<", "; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
int partition(int A[], int low, int high) | ||
{ | ||
int pivot = A[low]; | ||
int i = low + 1; | ||
int j = high; | ||
int temp; | ||
|
||
do | ||
{ | ||
while (A[i] <= pivot) | ||
{ | ||
i++; | ||
} | ||
|
||
while (A[j] > pivot) | ||
{ | ||
j--; | ||
} | ||
|
||
if (i < j) | ||
{ | ||
temp = A[i]; | ||
A[i] = A[j]; | ||
A[j] = temp; | ||
} | ||
} while (i < j); | ||
|
||
// Swap A[low] and A[j] | ||
temp = A[low]; | ||
A[low] = A[j]; | ||
A[j] = temp; | ||
return j; | ||
} | ||
|
||
void quickSort(int A[], int low, int high) | ||
{ | ||
int partitionIndex; | ||
if (low < high) | ||
{ | ||
partitionIndex = partition(A, low, high); | ||
quickSort(A, low, partitionIndex - 1); | ||
quickSort(A, partitionIndex + 1, high); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
|
||
int A[] = {9, 4, 4, 8, 7, 5, 6}; | ||
|
||
int n = 9; | ||
n =7; | ||
printArray(A, n); | ||
quickSort(A, 0, n - 1); | ||
printArray(A, n); | ||
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,80 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
/* | ||
Best Time Complexity : O(nlogn) | ||
Average Time Complexity : O(nlogn) | ||
Worst Time Complexity : O(nlogn) | ||
Worst Space Complexity : O(n) | ||
*/ | ||
|
||
void printArray(int A[], int n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout << A[i] << ", "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
void merge(int A[], int mid, int low, int high) | ||
{ | ||
int i, j, k, B[100]; | ||
i = low; | ||
j = mid + 1; | ||
k = low; | ||
|
||
while (i <= mid && j <= high) | ||
{ | ||
if (A[i] < A[j]) | ||
{ | ||
B[k] = A[i]; | ||
i++; | ||
k++; | ||
} | ||
else | ||
{ | ||
B[k] = A[j]; | ||
j++; | ||
k++; | ||
} | ||
} | ||
while (i <= mid) | ||
{ | ||
B[k] = A[i]; | ||
k++; | ||
i++; | ||
} | ||
while (j <= high) | ||
{ | ||
B[k] = A[j]; | ||
k++; | ||
j++; | ||
} | ||
for (int i = low; i <= high; i++) | ||
{ | ||
A[i] = B[i]; | ||
} | ||
} | ||
|
||
void mergeSort(int A[], int low, int high) | ||
{ | ||
int mid; | ||
if (low < high) | ||
{ | ||
mid = (low + high) / 2; | ||
mergeSort(A, low, mid); | ||
mergeSort(A, mid + 1, high); | ||
merge(A, mid, low, high); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int A[] = {9, 1, 4, 14, 4, 15, 6}; | ||
int n = 7; | ||
printArray(A, n); | ||
mergeSort(A, 0, 6); | ||
printArray(A, n); | ||
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,50 @@ | ||
#include <iostream> | ||
using namespace std; | ||
void heapify(int a[], int n, int i) { | ||
int largest = i; | ||
int left = 2 * i + 1; | ||
int right = 2 * i + 2; | ||
|
||
if (left < n && a[left] > a[largest]) | ||
largest = left; | ||
|
||
if (right < n && a[right] > a[largest]) | ||
largest = right; | ||
if (largest != i) { | ||
int temp = a[i]; | ||
a[i] = a[largest]; | ||
a[largest] = temp; | ||
|
||
heapify(a, n, largest); | ||
} | ||
} | ||
|
||
void heapSort(int a[], int n) { | ||
for (int i = n / 2 - 1; i >= 0; i--) | ||
heapify(a, n, i); | ||
for (int i = n - 1; i >= 0; i--) { | ||
int temp = a[0]; | ||
a[0] = a[i]; | ||
a[i] = temp; | ||
|
||
heapify(a, i, 0); | ||
} | ||
} | ||
|
||
void printArr(int a[], int n) { | ||
for (int i = 0; i < n; i++){ | ||
cout<<a[i]<<" "; | ||
} | ||
|
||
} | ||
|
||
int main() { | ||
int a[] = {47, 9, 22, 42, 27, 25, 0}; | ||
int n = sizeof(a) / sizeof(a[0]); | ||
cout<<"Before sorting array elements are - \n"; | ||
printArr(a, n); | ||
heapSort(a, n); | ||
cout<<"\nAfter sorting array elements are - \n"; | ||
printArr(a, n); | ||
return 0; | ||
} |
Oops, something went wrong.