mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-28 23:15:33 +08:00
Sorting
This commit is contained in:
37
Sorting/Bubble Sort.cpp
Normal file
37
Sorting/Bubble Sort.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//Bubble Sort
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int Array[6];
|
||||
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
|
||||
//Input
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
cin>>Array[i];
|
||||
}
|
||||
|
||||
//Bubble Sorting
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
for(int j=0; j<5; j++)
|
||||
{
|
||||
if(Array[j]>Array[j+1])
|
||||
{
|
||||
int temp=Array[j];
|
||||
Array[j]=Array[j+1];
|
||||
Array[j+1]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Output
|
||||
cout<<"\nSorted Array : ";
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
cout<<Array[i]<<"\t";
|
||||
}
|
||||
}
|
||||
62
Sorting/Heap Sort .cpp
Normal file
62
Sorting/Heap Sort .cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
using namespace std;
|
||||
void max_heapify(int *a, int i, int n)
|
||||
{
|
||||
int j, temp;
|
||||
temp = a[i];
|
||||
j = 2 * i;
|
||||
while (j <= n)
|
||||
{
|
||||
if (j < n && a[j + 1] > a[j])
|
||||
j = j + 1;
|
||||
if (temp > a[j])
|
||||
break;
|
||||
else if (temp <= a[j])
|
||||
{
|
||||
a[j / 2] = a[j];
|
||||
j = 2 * j;
|
||||
}
|
||||
}
|
||||
a[j / 2] = temp;
|
||||
return;
|
||||
}
|
||||
void heapsort(int *a, int n)
|
||||
{
|
||||
int i, temp;
|
||||
for (i = n; i >= 2; i--)
|
||||
{
|
||||
temp = a[i];
|
||||
a[i] = a[1];
|
||||
a[1] = temp;
|
||||
max_heapify(a, 1, i - 1);
|
||||
}
|
||||
}
|
||||
void build_maxheap(int *a, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = n / 2; i >= 1; i--)
|
||||
{
|
||||
max_heapify(a, i, n);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, i, x;
|
||||
cout << "Enter number of elements of array\n";
|
||||
cin >> n;
|
||||
int a[20];
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
cout << "Enter Element " << (i) << endl;
|
||||
cin >> a[i];
|
||||
}
|
||||
build_maxheap(a, n);
|
||||
heapsort(a, n);
|
||||
cout << "Sorted Output\n";
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
cout << a[i] << endl;
|
||||
}
|
||||
getch();
|
||||
}
|
||||
38
Sorting/Insertion Sort.cpp
Normal file
38
Sorting/Insertion Sort.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//Insertion Sort
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int Array[n];
|
||||
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
|
||||
//Input
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
cin>>Array[i];
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=1; i<n; i++)
|
||||
{
|
||||
int temp=Array[i];
|
||||
int j=i-1;
|
||||
while(j>=0 && temp<Array[j])
|
||||
{
|
||||
Array[j+1]=Array[j];
|
||||
j--;
|
||||
}
|
||||
Array[j+1]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
cout<<"\nSorted Array : ";
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
cout<<Array[i]<<"\t";
|
||||
}
|
||||
}
|
||||
|
||||
99
Sorting/Merge Sort.cpp
Normal file
99
Sorting/Merge Sort.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
void merge(int arr[], int l, int m, int r)
|
||||
{
|
||||
int i, j, k;
|
||||
int n1 = m - l + 1;
|
||||
int n2 = r - m;
|
||||
|
||||
int L[n1], R[n2];
|
||||
|
||||
|
||||
for (i = 0; i < n1; i++)
|
||||
L[i] = arr[l + i];
|
||||
for (j = 0; j < n2; j++)
|
||||
R[j] = arr[m + 1+ j];
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
k = l;
|
||||
while (i < n1 && j < n2)
|
||||
{
|
||||
if (L[i] <= R[j])
|
||||
{
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
while (i < n1)
|
||||
{
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
|
||||
while (j < n2)
|
||||
{
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mergeSort(int arr[], int l, int r)
|
||||
{
|
||||
if (l < r)
|
||||
{
|
||||
|
||||
int m = l+(r-l)/2;
|
||||
|
||||
|
||||
mergeSort(arr, l, m);
|
||||
mergeSort(arr, m+1, r);
|
||||
|
||||
merge(arr, l, m, r);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void show(int A[], int size)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < size; i++)
|
||||
cout<<A[i]<<"\n";
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int size;
|
||||
cout<<"\nEnter the number of elements : ";
|
||||
|
||||
cin>>size;
|
||||
|
||||
int arr[size];
|
||||
|
||||
cout<<"\nEnter the unsorted elements : ";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
cout<<"\n";
|
||||
cin>>arr[i];
|
||||
}
|
||||
|
||||
mergeSort(arr, 0, size);
|
||||
|
||||
cout<<"Sorted array\n";
|
||||
show(arr, size);
|
||||
return 0;
|
||||
}
|
||||
69
Sorting/Quick Sort.cpp
Normal file
69
Sorting/Quick Sort.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/* C implementation QuickSort */
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int partition (int arr[], int low, int high)
|
||||
{
|
||||
int pivot = arr[high]; // pivot
|
||||
int i = (low - 1); // Index of smaller element
|
||||
|
||||
for (int j = low; j <high; j++)
|
||||
{
|
||||
// If current element is smaller than or
|
||||
// equal to pivot
|
||||
if (arr[j] <= pivot)
|
||||
{
|
||||
i++; // increment index of smaller element
|
||||
int temp=arr[i];
|
||||
arr[i]=arr[j];
|
||||
arr[j]=temp;
|
||||
}
|
||||
}
|
||||
int temp=arr[i+1];
|
||||
arr[i+1]=arr[high];
|
||||
arr[high]=temp;
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
|
||||
void quickSort(int arr[], int low, int high)
|
||||
{
|
||||
if (low < high)
|
||||
{
|
||||
|
||||
int p= partition(arr, low, high);
|
||||
|
||||
quickSort(arr, low, p - 1);
|
||||
quickSort(arr, p + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void show(int arr[], int size)
|
||||
{
|
||||
for (int i=0; i < size; i++)
|
||||
cout<<arr[i]<<"\n";
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
int size;
|
||||
cout<<"\nEnter the number of elements : ";
|
||||
|
||||
cin>>size;
|
||||
|
||||
int arr[size];
|
||||
|
||||
cout<<"\nEnter the unsorted elements : ";
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
cout<<"\n";
|
||||
cin>>arr[i];
|
||||
}
|
||||
quickSort(arr, 0, size);
|
||||
cout<<"Sorted array\n";
|
||||
show(arr, size);
|
||||
return 0;
|
||||
}
|
||||
60
Sorting/Radix Sort.cpp
Normal file
60
Sorting/Radix Sort.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
void radixsort(int a[],int n){
|
||||
int count[10];
|
||||
int output[n];
|
||||
memset(output,0,sizeof(output));
|
||||
memset(count,0,sizeof(count));
|
||||
int max = 0;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i]>max)
|
||||
{
|
||||
max = a[i];
|
||||
}
|
||||
}
|
||||
int maxdigits = 0;
|
||||
while(max){
|
||||
maxdigits++;
|
||||
max/=10;
|
||||
}
|
||||
for(int j=0;j<maxdigits;j++){
|
||||
for(int i=0;i<n;i++){
|
||||
int t = pow(10,j);
|
||||
count[(a[i]%(10*t))/t]++;
|
||||
}
|
||||
int k = 0;
|
||||
for(int p=0;p<10;p++){
|
||||
for(int i=0;i<n;i++){
|
||||
int t = pow(10,j);
|
||||
if((a[i]%(10*t))/t==p){
|
||||
output[k] = a[i];
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
memset(count,0,sizeof(count));
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
a[i] = output[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
void print(int a[],int n){
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cout<<a[i]<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int a[] = {170, 45, 75, 90, 802, 24, 2, 66};
|
||||
int n = sizeof(a)/sizeof(a[0]);
|
||||
radixsort(a,n);
|
||||
print(a,n);
|
||||
return 0;
|
||||
}
|
||||
39
Sorting/Selection Sort.cpp
Normal file
39
Sorting/Selection Sort.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
//Selection Sort
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int Array[6];
|
||||
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
|
||||
//Input
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
cin>>Array[i];
|
||||
}
|
||||
|
||||
//Selection Sorting
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
int min=i;
|
||||
for(int j=i+1; j<6; j++)
|
||||
{
|
||||
if(Array[j]<Array[min])
|
||||
{
|
||||
min=j; //Finding the smallest number in Array
|
||||
}
|
||||
}
|
||||
int temp =Array[i];
|
||||
Array[i]=Array[min];
|
||||
Array[min]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
cout<<"\nSorted Array : ";
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
cout<<Array[i]<<"\t";
|
||||
}
|
||||
}
|
||||
45
Sorting/Shell Sort.cpp
Normal file
45
Sorting/Shell Sort.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int size=10;
|
||||
int array[size];
|
||||
// Input
|
||||
cout<<"\nHow many numbers do want to enter in unsorted array : ";
|
||||
cin>>size;
|
||||
cout<<"\nEnter the numbers for unsorted array : ";
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
cin>>array[i];
|
||||
}
|
||||
|
||||
// Sorting
|
||||
for (int i = size/2; i>0 ; i=i/2)
|
||||
{
|
||||
for (int j = i; j <size ; j++)
|
||||
{
|
||||
for (int k = j-i; k>=0; k=k-i)
|
||||
{
|
||||
if (array[k]<array[k+i])
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
int temp=array[k+i];
|
||||
array[k+i]=array[k];
|
||||
array[k]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
cout<<"\nSorted array : ";
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
cout<<array[i]<<"\t";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user