Skip to content

Sorting algorithms #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Sorting algorithms/BUBBLESO.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//Bubble sort Program Time complexity is O(n^2)

#include<iostream>
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"<<a[i]; //prints the sorted element in order
}
return 0;
}
55 changes: 55 additions & 0 deletions Sorting algorithms/QUICKSOR.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//quick sort program time complexity in average case O(nlogn)

#include<iostream>
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[i];
}
return 0;
}

void
quicksort (int a[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (a[i] <= a[pivot] && i <= high)
{
i++;
}
while (a[j] > 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);
}
}
35 changes: 35 additions & 0 deletions Sorting algorithms/SELECTIO.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Selection sort algorithm works with an time complexity of O(n^2)
#include<iostream>
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"<<a[i];
}
return 0;
}
69 changes: 69 additions & 0 deletions Sorting algorithms/SHELLSOR.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

// Shell sort algorithm time complexity is best case O(nlogn) and worst case O(n^2)

#include<iostream>
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;
}