diff --git a/Sorting algorithms/BUBBLESO.CPP b/Sorting algorithms/BUBBLESO.CPP new file mode 100644 index 0000000..3cebb56 --- /dev/null +++ b/Sorting algorithms/BUBBLESO.CPP @@ -0,0 +1,36 @@ +//Bubble sort Program Time complexity is O(n^2) + +#include +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"< +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.CPP b/Sorting algorithms/SELECTIO.CPP new file mode 100644 index 0000000..68f96d2 --- /dev/null +++ b/Sorting algorithms/SELECTIO.CPP @@ -0,0 +1,35 @@ +//Selection sort algorithm works with an time complexity of O(n^2) +#include +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"< +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 < n; i++) + { + cin >> a[i]; + } + } + void shellsorting () + { + + 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 display () + { + for (i = 0; i < n; i++) + { + cout << a[i] << "\t"; + } + } +}; + +int main () +{ + + shellsort obj; + obj.getdata (); + cout << "before sorting:"; + obj.display (); + obj.shellsorting (); + cout << endl << "after sorting:"; + obj.display (); + return 0; +} \ No newline at end of file