From edb80be131f4454c475cb4cd142328c4fd16c344 Mon Sep 17 00:00:00 2001 From: AyeshaNoor786 Date: Thu, 13 Oct 2022 13:17:31 +0500 Subject: [PATCH] Sorting Algorithm in C++ --- HelloWorld/helloworld.cpp | 50 ++++++- algorithms/SortingAlgorithms/1selSort.cpp | 59 ++++++++ algorithms/SortingAlgorithms/2bblSort.cpp | 55 +++++++ algorithms/SortingAlgorithms/3insSort.cpp | 48 ++++++ algorithms/SortingAlgorithms/4mrgSort.cpp | 87 +++++++++++ .../5selSort-counting-instructions.cpp | 92 ++++++++++++ .../6mrgSort-counting-instructions.cpp | 137 ++++++++++++++++++ algorithms/SortingAlgorithms/7bblSort.cpp | 49 +++++++ 8 files changed, 572 insertions(+), 5 deletions(-) create mode 100644 algorithms/SortingAlgorithms/1selSort.cpp create mode 100644 algorithms/SortingAlgorithms/2bblSort.cpp create mode 100644 algorithms/SortingAlgorithms/3insSort.cpp create mode 100644 algorithms/SortingAlgorithms/4mrgSort.cpp create mode 100644 algorithms/SortingAlgorithms/5selSort-counting-instructions.cpp create mode 100644 algorithms/SortingAlgorithms/6mrgSort-counting-instructions.cpp create mode 100644 algorithms/SortingAlgorithms/7bblSort.cpp diff --git a/HelloWorld/helloworld.cpp b/HelloWorld/helloworld.cpp index b759adf..7acc3c3 100644 --- a/HelloWorld/helloworld.cpp +++ b/HelloWorld/helloworld.cpp @@ -1,8 +1,48 @@ -#include -#include +#include +#include +#include -int main() -{ - printf("Hello World!"); +using namespace std; + +vector Census; + +void Census2017() { + /* + * Add yourself using the following format: + * Census.push_back("Your Name @ https://github.com/username"); + */ + + Census.push_back("Allen Comp Sci @ https://github.com/AllenCompSci"); + Census.push_back("Mr. Hudson @ https://github.com/theshrewedshrew"); + Census.push_back("BEST Team 58 @ https://github.com/BESTTeam58"); + Census.push_back("TexasSnow @ https://github.com/TexasSnow"); + Census.push_back("hotdogshabab @ https://github.com/hotdogshabab"); + Census.push_back("alansunglee @ https://github.com/alansunglee"); + Census.push_back("Rahultheman12 @ https://github.com/Rahultheman12"); + Census.push_back("spicyboi @ https://github.com/spicyboi"); + Census.push_back("John Nguyen @ https://github.com/jawnlovesfreestuff"); + Census.push_back("CodeTimesTen @ https://github.com/CodeTimesTen"); + Census.push_back("YourFriendlyNeighborhoodSpiderman @ https://github.com/YourFriendlyNeighborhoodSpiderman"); + Census.push_back("Devin Petersen @ https://github.com/DevinPetersen"); + Census.push_back("Cameron Mathis @ https://github.com/Phylux"); + Census.push_back("Samuel Woon @ https://github.com/samuel-w"); + Census.push_back("JustinV10 @ https://github.com/JustinV10"); + Census.push_back("Kyleaustin36 @ https://github.com/kyleaustin36"); + Census.push_back("Maaz Kamal @ https://github.com/Maze-Camel"); + Census.push_back("bingood4ever @ https://github.com/bingood4ever"); + Census.push_back("Gainz101 @ https://github.com/Gainz101"); + Census.push_back("zachdogg @ https://github.com/Zachdogg1"); + Census.push_back("PJHudson618 @ https://github.com/PJHudson618"); + Census.push_back("Abhi1458 @ https://github.com/Abhi1458"); + Census.push_back("Maxwell Cody @ https://github.com/MaxwellCody"); } +int main() { + Census2017(); + + for(int i = 0; i < (int) Census.size(); ++i) { + cout << "Hello World from " << Census[i] << endl; + } + + return 0; +} diff --git a/algorithms/SortingAlgorithms/1selSort.cpp b/algorithms/SortingAlgorithms/1selSort.cpp new file mode 100644 index 0000000..384e39c --- /dev/null +++ b/algorithms/SortingAlgorithms/1selSort.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +void swap(int a[], int x, int y) +{ + int t=a[x]; + a[x]=a[y]; + a[y]=t; +} + +int locOfSmallest(int a[], int s, int e) +{ + int i = s; + int j = i; + while(i <= e) + { + if(a[i] < a[j]) + { + j = i; + } + i = i+1; + } + return j; +} + +void selSort(int a[], int n) +{ + int i= 0; + while(i < n-1) + { + int j = locOfSmallest(a,i,n-1); + swap(a, i, j); + i = i+1; + } +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +int main() +{ + int arr[] = {22,66,44,27,967,34,2,90,4567,21,75,9,44}; + + + show(arr, sizeof(arr)/sizeof(int)); + selSort(arr, sizeof(arr)/sizeof(int)); + show(arr, sizeof(arr)/sizeof(int)); + + return 0; +} \ No newline at end of file diff --git a/algorithms/SortingAlgorithms/2bblSort.cpp b/algorithms/SortingAlgorithms/2bblSort.cpp new file mode 100644 index 0000000..1321b68 --- /dev/null +++ b/algorithms/SortingAlgorithms/2bblSort.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +void swap(int a[], int x, int y) +{ + int t=a[x]; + a[x]=a[y]; + a[y]=t; +} + +void bubble(int a[], int n) +{ + int i = n-1; + while(i > 0) + { + if(a[i] < a[i-1]) + { + swap(a,i,i-1); + } + i = i-1; + } +} + +void bblSort(int a[], int n) +{ + int i= 0; + while(i < n-1) + { + bubble(a,n); + i = i+1; + } +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +int main() +{ + int arr[] = {22,66,44,27,967,34,2,90,4567,21,75,9,44}; + + show(arr, sizeof(arr)/sizeof(int)); + bblSort(arr, sizeof(arr)/sizeof(int)); + show(arr, sizeof(arr)/sizeof(int)); + + return 0; +} \ No newline at end of file diff --git a/algorithms/SortingAlgorithms/3insSort.cpp b/algorithms/SortingAlgorithms/3insSort.cpp new file mode 100644 index 0000000..9160afc --- /dev/null +++ b/algorithms/SortingAlgorithms/3insSort.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +void insertIth(int a[], int n, int i) +{ + int key = a[i]; + int j = i-1; + while(j >= 0 && a[j] > key) + { + a[j+1] = a[j]; + j = j-1; + } + a[j+1] = key; +} + +void insSort(int a[], int n) +{ + int i = 1; // second element + while(i < n) + { + insertIth(a,n,i); + i = i+1; + } +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +int main() +{ + int arr[] = {22,66,44,27,967,34,2,90,4567,21,75,9,44}; + int arrSize = sizeof(arr)/sizeof(int); + + show(arr, arrSize); + insSort(arr, arrSize); + show(arr, arrSize); + + return 0; +} \ No newline at end of file diff --git a/algorithms/SortingAlgorithms/4mrgSort.cpp b/algorithms/SortingAlgorithms/4mrgSort.cpp new file mode 100644 index 0000000..337e678 --- /dev/null +++ b/algorithms/SortingAlgorithms/4mrgSort.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +void combine(int a[], int s, int m, int e) +{ + int *buffer = new int [e+1]; + + int k = s; + while(k <= e) + { + buffer[k] = a[k]; + k = k + 1; + } + + int i = s; + int j = m+1; + k = s; + while(i <= m && j <= e) + { + if(buffer[i] <= buffer[j]) + { + a[k] = buffer[i]; + i = i + 1; + } + else + { + a[k] = buffer[j]; + j = j + 1; + } + k = k + 1; + } + while(i <= m) + { + a[k] = buffer[i]; + i = i + 1; + k = k + 1; + } + while(j <= e) + { + a[k] = buffer[j]; + j = j + 1; + k = k + 1; + } + + delete[] buffer; +} + +void mrgSort(int a[], int s, int e) +{ + if (s == e) + { + return; + } + int m = (s+e)/2; + mrgSort(a, s, m); + mrgSort(a, m+1, e); + combine(a,s,m,e); +} + +void mrgSort(int a[], int n) +{ + mrgSort(a,0,n-1); +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +int main() +{ + int arr[] = {22,66,44,27,967,34,2,90,4567,21,75,9,44}; + int arrSize = sizeof(arr)/sizeof(int); + + show(arr, arrSize); + mrgSort(arr, arrSize); + show(arr, arrSize); + + return 0; +} \ No newline at end of file diff --git a/algorithms/SortingAlgorithms/5selSort-counting-instructions.cpp b/algorithms/SortingAlgorithms/5selSort-counting-instructions.cpp new file mode 100644 index 0000000..ede625f --- /dev/null +++ b/algorithms/SortingAlgorithms/5selSort-counting-instructions.cpp @@ -0,0 +1,92 @@ +#include +#include + +using namespace std; + +int count = 0; + +void swap(int a[], int x, int y) +{ + ::count++; // count next line + int t=a[x]; + ::count++; // count next line + a[x]=a[y]; + ::count++; // count next line + a[y]=t; +} + +int locOfSmallest(int a[], int s, int e) +{ + ::count++; // count next line + int i = s; + ::count++; // count next line + int j = i; + ::count++; // count next line + while(i <= e) + { + ::count++; // count next line + if(a[i] < a[j]) + { + ::count++; // count next line + j = i; + } + ::count++; // count next line + i = i+1; + ::count++; // count while condition test + } + ::count++; // count next line + return j; +} + +void selSort(int a[], int n) +{ + ::count++; // count next line + int i= 0; + ::count++; // count next line + while(i < n-1) + { + ::count++; // count next line + int j = locOfSmallest(a,i,n-1); + swap(a, i, j); // it has its own count + ::count++; // count next line + i = i+1; + ::count++; // count while condition test + } +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +void fillArray(int a[], int n) +{ + int i = 0; + while(i < n) + { + a[i] = rand()%99999+rand()%99; + i = i+1; + } +} + +int main() +{ + ::count = 0 ; // global variable + + int arr[100000]; + fillArray(arr, sizeof(arr)/sizeof(int)); + + //show(arr, sizeof(arr)/sizeof(int)); + selSort(arr, sizeof(arr)/sizeof(int)); + //show(arr, sizeof(arr)/sizeof(int)); + + cout << "Steps taken during sorting are " << ::count << endl; + + return 0; +} \ No newline at end of file diff --git a/algorithms/SortingAlgorithms/6mrgSort-counting-instructions.cpp b/algorithms/SortingAlgorithms/6mrgSort-counting-instructions.cpp new file mode 100644 index 0000000..ca1297a --- /dev/null +++ b/algorithms/SortingAlgorithms/6mrgSort-counting-instructions.cpp @@ -0,0 +1,137 @@ +#include +#include + +using namespace std; + +int count = 0; + +void combine(int a[], int s, int m, int e) +{ + ::count++; // count next line + int *buffer = new int [e+1]; + + ::count++; // count next line + int k = s; + ::count++; // count next line + while(k <= e) + { + ::count++; // count next line + buffer[k] = a[k]; + ::count++; // count next line + k = k + 1; + ::count++; // count while condition test + } + + ::count++; // count next line + int i = s; + ::count++; // count next line + int j = m+1; + ::count++; // count next line + k = s; + ::count++; // count next line + while(i <= m && j <= e) + { + ::count++; // count next line + if(buffer[i] <= buffer[j]) + { + ::count++; // count next line + a[k] = buffer[i]; + ::count++; // count next line + i = i + 1; + } + else + { + ::count++; // count next line + a[k] = buffer[j]; + ::count++; // count next line + j = j + 1; + } + ::count++; // count next line + k = k + 1; + ::count++; // count while condition test + } + ::count++; // count next line + while(i <= m) + { + ::count++; // count next line + a[k] = buffer[i]; + ::count++; // count next line + i = i + 1; + ::count++; // count next line + k = k + 1; + ::count++; // count while condition test + } + ::count++; // count next line + while(j <= e) + { + ::count++; // count next line + a[k] = buffer[j]; + ::count++; // count next line + j = j + 1; + ::count++; // count next line + k = k + 1; + ::count++; // count while condition test + } + + ::count++; // count next line + delete[] buffer; +} + +void mrgSort(int a[], int s, int e) +{ + ::count++; // count next line + if (s >= e) + { + ::count++; // count next line + return; + } + ::count++; // count next line + int m = (s+e)/2; + mrgSort(a, s, m); // it has its own count + mrgSort(a, m+1, e); // it has its own count + combine(a,s,m,e); // it has its own count +} + +void mrgSort(int a[], int n) +{ + mrgSort(a,0,n-1); +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +void fillArray(int a[], int n) +{ + int i = 0; + while(i < n) + { + a[i] = rand()%99999+rand()%99; + i = i+1; + } + cout << endl; +} + +int main() +{ + ::count = 0 ; // global variable + + int arr[100000]; + int arrSize = sizeof(arr)/sizeof(int); + fillArray(arr, arrSize); + + //show(arr, arrSize); + mrgSort(arr, arrSize); + //show(arr, arrSize); + + cout << "Steps taken during sorting are " << ::count << endl; + + return 0; +} \ No newline at end of file diff --git a/algorithms/SortingAlgorithms/7bblSort.cpp b/algorithms/SortingAlgorithms/7bblSort.cpp new file mode 100644 index 0000000..2461bd9 --- /dev/null +++ b/algorithms/SortingAlgorithms/7bblSort.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +void bblSort(int a[], int n) +{ + int j= 0; + int i; + int t; + while(j < n-1) + { + //bubble(a,n); + i = n-1; + while(i > j) + { + if(a[i] < a[i-1]) + { + //swap(a,i,i-1); + t=a[i]; + a[i]=a[i-1]; + a[i-1]=t; + } + i = i-1; + } + j = j+1; + } +} + +void show(int a[], int n) +{ + int i = 0; + while(i < n) + { + cout << a[i] << ", "; + i = i+1; + } + cout << endl; +} + +int main() +{ + int arr[] = {22,66,44,27,967,34,2,90,4567,21,75,9,44}; + + show(arr, sizeof(arr)/sizeof(int)); + bblSort(arr, sizeof(arr)/sizeof(int)); + show(arr, sizeof(arr)/sizeof(int)); + + return 0; +} \ No newline at end of file