From bc658b34b0b0db09103e57e4465aa53badb60ef6 Mon Sep 17 00:00:00 2001 From: saiharsha-22 Date: Thu, 1 Oct 2020 16:39:31 +0530 Subject: [PATCH 1/2] Sorting algorithms --- Sorting algorithms/BUBBLESO.C | 31 ++++++++++++++++ Sorting algorithms/QUICKSOR.C | 53 +++++++++++++++++++++++++++ Sorting algorithms/SELECTIO.C | 34 +++++++++++++++++ Sorting algorithms/SHELLSOR.CPP | 65 +++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 Sorting algorithms/BUBBLESO.C create mode 100644 Sorting algorithms/QUICKSOR.C create mode 100644 Sorting algorithms/SELECTIO.C create mode 100644 Sorting algorithms/SHELLSOR.CPP diff --git a/Sorting algorithms/BUBBLESO.C b/Sorting algorithms/BUBBLESO.C new file mode 100644 index 0000000..586293a --- /dev/null +++ b/Sorting algorithms/BUBBLESO.C @@ -0,0 +1,31 @@ +#include +#include +void main() +{ +clrscr(); +int n,a[50],i,j,temp; +printf("enter number of elements that you want to store:\n"); +scanf("%d",&n); +for(i=0;ia[j]) //if you change the relational operator u will get desceding order +{ +temp=a[i]; +a[i]=a[j]; +a[j]=temp; +} +} +} +printf("acending order of the elements is:"); +for(i=0;i +#include +void quicksort(int[],int,int); +void main() +{ +int a[50],n,i; +clrscr(); +printf("enter number of elemnts that u want to store:\n"); +scanf("%d",&n); +for(i=0;ia[pivot] && j>=low) + { + j--; + } + if(i +#include +void main() +{ +int a[50],n,i,j,min,temp; +printf("enter number of elements that u want to store:\n"); +scanf("%d",&n); +for(i=0;ia[j]) +{ +min=j; +} +} +if(min!=i) +{ +temp=a[i]; +a[i]=a[min]; +a[min]=temp; +} +} +for(i=0;i +#include +class shellsort +{ + private: + int a[20],n,i,j,gap,temp; + public: + void getdata() + { + cout<<"enter number of elemnts:\n"; + cin>>n; + cout<<"enter the elements:\n"; + for(i=0;i>a[i]; + } + } + void shellsorting() + { + + for(gap=n/2;gap>0;gap=gap/2) + { + for(j=gap;j=0;i-=gap) + { + if(a[i+gap]>a[i]) + { + break; + } + else + { + swap(); + } + } + } + } +} + void swap() + { + temp=a[i+gap]; + a[i+gap]=a[i]; + a[i]=temp; + + } +void display() +{ + for(i=0;i Date: Thu, 1 Oct 2020 17:30:36 +0530 Subject: [PATCH 2/2] Sorting alogorithms --- Sorting algorithms/BUBBLESO.C | 31 --------- Sorting algorithms/BUBBLESO.CPP | 36 ++++++++++ Sorting algorithms/QUICKSOR.C | 53 --------------- Sorting algorithms/QUICKSOR.CPP | 55 +++++++++++++++ Sorting algorithms/SELECTIO.C | 34 ---------- Sorting algorithms/SELECTIO.CPP | 35 ++++++++++ Sorting algorithms/SHELLSOR.CPP | 116 +++++++++++++++++--------------- 7 files changed, 186 insertions(+), 174 deletions(-) delete mode 100644 Sorting algorithms/BUBBLESO.C create mode 100644 Sorting algorithms/BUBBLESO.CPP delete mode 100644 Sorting algorithms/QUICKSOR.C create mode 100644 Sorting algorithms/QUICKSOR.CPP delete mode 100644 Sorting algorithms/SELECTIO.C create mode 100644 Sorting algorithms/SELECTIO.CPP diff --git a/Sorting algorithms/BUBBLESO.C b/Sorting algorithms/BUBBLESO.C deleted file mode 100644 index 586293a..0000000 --- a/Sorting algorithms/BUBBLESO.C +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -void main() -{ -clrscr(); -int n,a[50],i,j,temp; -printf("enter number of elements that you want to store:\n"); -scanf("%d",&n); -for(i=0;ia[j]) //if you change the relational operator u will get desceding order -{ -temp=a[i]; -a[i]=a[j]; -a[j]=temp; -} -} -} -printf("acending order of the elements is:"); -for(i=0;i +using namespace std; + +int main () +{ + + int n, a[50], i, j, temp; + //Taking the input from the user + cout<<"enter number of elements that you want to store:\n"; + cin>>n; + + for (i = 0; i < n; i++) + { + cin>>a[i]; // stores the values in the arrays. + } + for (i = 0; i < n - 1; i++) + { + for (j = i + 1; j < n; j++) + { + if (a[i] > a[j]) //if you change the relational operator u will get desceding order + { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + } + } + cout<<"acending order of the elements is:"; + for (i = 0; i < n; i++) + { + cout<<"\t"< -#include -void quicksort(int[],int,int); -void main() -{ -int a[50],n,i; -clrscr(); -printf("enter number of elemnts that u want to store:\n"); -scanf("%d",&n); -for(i=0;ia[pivot] && j>=low) - { - j--; - } - if(i +using namespace std; +void quicksort (int[], int, int); +int main () +{ + int a[50], n, i; + cout<<"enter number of elemnts that u want to store:\n"; + cin>>n; + for (i = 0; i < n; i++) + { + cin>>a[i]; + } + quicksort (a, 0, n - 1); + for (i = 0; i < n; i++) + { + cout<<"\t"< a[pivot] && j >= low) + { + j--; + } + if (i < j) + { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + } + temp = a[j]; + a[j] = a[pivot]; + a[pivot] = temp; + quicksort (a, low, j - 1); + quicksort (a, j + 1, high); + } +} diff --git a/Sorting algorithms/SELECTIO.C b/Sorting algorithms/SELECTIO.C deleted file mode 100644 index cdd23ce..0000000 --- a/Sorting algorithms/SELECTIO.C +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -void main() -{ -int a[50],n,i,j,min,temp; -printf("enter number of elements that u want to store:\n"); -scanf("%d",&n); -for(i=0;ia[j]) -{ -min=j; -} -} -if(min!=i) -{ -temp=a[i]; -a[i]=a[min]; -a[min]=temp; -} -} -for(i=0;i +using namespace std; +int main () +{ + int a[50], n, i, j, min, temp; + cout<<"enter number of elements that u want to store:\n"; + cin>>n; + for (i = 0; i < n; i++) + { + cin>>a[i]; + } + for (i = 0; i < n - 1; i++) + { + min = i; + for (j = i + 1; j < n; j++) + { + if (a[min] > a[j]) + { + min = j; + } + } + if (min != i) + { + temp = a[i]; + a[i] = a[min]; + a[min] = temp; + } + } + for (i = 0; i < n; i++) + { + cout<<"\t"< -#include + +// Shell sort algorithm time complexity is best case O(nlogn) and worst case O(n^2) + +#include +using namespace std; class shellsort { - private: - int a[20],n,i,j,gap,temp; - public: - void getdata() - { - cout<<"enter number of elemnts:\n"; - cin>>n; - cout<<"enter the elements:\n"; - for(i=0;i>a[i]; + cout << "enter number of elemnts:\n"; + cin >> n; + cout << "enter the elements:\n"; + for (i = 0; i < n; i++) + { + cin >> a[i]; + } } - } - void shellsorting() - { - - for(gap=n/2;gap>0;gap=gap/2) - { - for(j=gap;j=0;i-=gap) - { - if(a[i+gap]>a[i]) - { - break; - } - else - { - swap(); - } - } + + for (gap = n / 2; gap > 0; gap = gap / 2) + { + for (j = gap; j < n; j++) + { + for (i = j - gap; i >= 0; i -= gap) + { + if (a[i + gap] > a[i]) + { + break; + } + else + { + swap (); + } + } + } + } } - } -} - void swap() - { - temp=a[i+gap]; - a[i+gap]=a[i]; - a[i]=temp; + void swap () + { + temp = a[i + gap]; + a[i + gap] = a[i]; + a[i] = temp; - } -void display() -{ - for(i=0;i