forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fineanmol#3468 from DeveHk/master
Sortings
- Loading branch information
Showing
20 changed files
with
465 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void display(vector<int> A, int n) | ||
{ | ||
cout << endl; | ||
for (auto i : A) | ||
cout << i << " "; | ||
} | ||
|
||
void B_S(vector<int> &A, int n) | ||
{ | ||
for (int i = 0; i < n - 1; i++) | ||
{ | ||
for (int j = 0; j < n - 1 - i; j++) | ||
{ | ||
if (A[j] > A[j + 1]) | ||
swap(A[j], A[j + 1]); | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> V; | ||
int ch, n; | ||
cout << "size: "; | ||
cin >> n; | ||
cout << "ENTER ARRY: "; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> ch; | ||
V.push_back(ch); | ||
} | ||
|
||
B_S(V, n); | ||
display(V, n); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void display(vector<int> A, int n) | ||
{ | ||
cout << endl; | ||
for (auto i : A) | ||
cout << i << " "; | ||
} | ||
|
||
void B_S(vector<int> &A, int n) | ||
{ | ||
vector<int> B[n]; | ||
for(int i=0;i<n;i++){ | ||
B[n*A[i]]= | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> V; | ||
int ch, n; | ||
cout << "size: "; | ||
cin >> n; | ||
cout << "ENTER ARRY: "; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> ch; | ||
V.push_back(ch); | ||
} | ||
|
||
B_S(V, n); | ||
display(V, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void bucketSort(float arr[], int n) | ||
{ | ||
|
||
vector<float> b[n]; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
int bi = n * arr[i]; // Index in bucket | ||
b[bi].push_back(arr[i]); | ||
} | ||
|
||
for (int i = 0; i < n; i++) | ||
sort(b[i].begin(), b[i].end()); | ||
|
||
int index = 0; | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < b[i].size(); j++) | ||
arr[index++] = b[i][j]; | ||
} | ||
|
||
int main() | ||
{ | ||
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
bucketSort(arr, n); | ||
|
||
cout << "Sorted array is \n"; | ||
for (int i = 0; i < n; i++) | ||
cout << arr[i] << " "; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define RANGE 255 | ||
#define ll long long int | ||
|
||
vector<ll> countSort(vector<ll> arr) | ||
{ | ||
ll max = INT_MIN; | ||
for (auto i : arr) | ||
{ | ||
if (i > max) | ||
max = i; | ||
} | ||
vector<ll> F(max + 1, 0); | ||
for (auto i : arr) | ||
F[i]++; | ||
// Commulative | ||
for (int i = 1; i <= arr.size(); i++) | ||
{ | ||
F[i] = F[i] + F[i - 1]; | ||
} | ||
vector<ll> ans(arr.size(), 0); | ||
for (int i = 0; i < arr.size(); i++) | ||
{ | ||
F[arr[i]]--; | ||
ans[F[arr[i]]] = arr[i]; | ||
} | ||
return ans; | ||
} | ||
|
||
int main() | ||
{ | ||
ll n; | ||
cin >> n; | ||
vector<ll> V(n); | ||
for (auto &i : V) | ||
cin >> i; | ||
V = countSort(V); | ||
cout << endl; | ||
for (auto i : V) | ||
cout << i << " "; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void display(vector<int> A, int n) | ||
{ | ||
cout << endl; | ||
for (auto i : A) | ||
cout << i << " "; | ||
} | ||
|
||
void I_S(vector<int> &A, int n) | ||
{ | ||
for (int i = 1; i < n; i++) | ||
{ | ||
int j = i - 1, key = A[i]; | ||
while (j >= 0 && A[j] > key) | ||
{ | ||
A[j + 1] = A[j]; | ||
j--; | ||
} | ||
A[j + 1] = key; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> V; | ||
int ch, n; | ||
cout << "size: "; | ||
cin >> n; | ||
cout << "ENTER ARRY: "; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> ch; | ||
V.push_back(ch); | ||
} | ||
|
||
I_S(V, n); | ||
display(V, n); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void display(vector<int> A, int n) | ||
{ | ||
cout << endl; | ||
for (auto i : A) | ||
cout << i << " "; | ||
} | ||
|
||
void merge(int f, int mid, int l, vector<int> &A){ | ||
vector<int> temp; | ||
int i=f,j=mid+1; | ||
while(i<=mid && j<=l) | ||
{ | ||
if(A[i]<A[j]) | ||
{ | ||
temp.push_back(A[i]); | ||
i++; | ||
} | ||
else if(A[i]>A[j]) | ||
{ | ||
temp.push_back(A[j]); | ||
j++; | ||
} | ||
else | ||
{ | ||
temp.push_back(A[i]); | ||
temp.push_back(A[j]); | ||
j++; | ||
i++; | ||
} | ||
} | ||
while(i<=mid) | ||
{ | ||
temp.push_back(A[i]); | ||
i++; | ||
} | ||
while(j<=l) | ||
{ | ||
temp.push_back(A[j]); | ||
j++; | ||
} | ||
|
||
for(auto i:temp){ | ||
A[f]=i; | ||
f++; | ||
} | ||
} | ||
void M_S(vector<int> &A, int n,int f,int l) | ||
{ | ||
if(f>=l) | ||
return; | ||
int mid=(f+l)/2; | ||
M_S(A,n,f,mid); | ||
M_S(A,n,mid+1,l); | ||
merge(f,mid,l,A); | ||
display(A, n); | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> V; | ||
int ch, n; | ||
cout << "size: "; | ||
cin >> n; | ||
cout << "ENTER ARRY: "; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> ch; | ||
V.push_back(ch); | ||
} | ||
|
||
M_S(V, n, 0, n-1); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void display(vector<int> A, int n) | ||
{ | ||
cout << endl; | ||
for (auto i : A) | ||
cout << i << " "; | ||
} | ||
|
||
void Q_S(vector<int> &A, int n,int f,int l) | ||
{ | ||
if(f>=l) | ||
return; | ||
if(l-f==1) | ||
{ | ||
if(A[l]<A[f]) | ||
swap(A[l],A[f]); | ||
return; | ||
} | ||
int pivot=A[l]; | ||
int i=l-1,j=f; | ||
while (i>=j) | ||
{ | ||
if(A[j]>pivot && A[i]<pivot) | ||
swap(A[j],A[i]); | ||
if(A[i]>pivot) | ||
i--; | ||
if(A[j]<pivot) | ||
j++; | ||
|
||
} | ||
swap(A[j],A[l]); | ||
int mid=j; | ||
Q_S(A,n,f,mid-1); | ||
Q_S(A,n,mid+1,l); | ||
display(A,n); | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> V; | ||
int ch, n; | ||
cout << "size: "; | ||
cin >> n; | ||
cout << "ENTER ARRY: "; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> ch; | ||
V.push_back(ch); | ||
} | ||
|
||
Q_S(V, n, 0, n-1); | ||
display(V, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int getMax(int arr[], int n) | ||
{ | ||
int mx = arr[0]; | ||
for (int i = 1; i < n; i++) | ||
if (arr[i] > mx) | ||
mx = arr[i]; | ||
return mx; | ||
} | ||
|
||
void countSort(int arr[], int n, int exp) | ||
{ | ||
int output[n]; | ||
int i, count[10] = { 0 }; | ||
for (i = 0; i < n; i++) | ||
count[(arr[i] / exp) % 10]++; | ||
for (i = 1; i < 10; i++) | ||
count[i] += count[i - 1]; | ||
for (i = n - 1; i >= 0; i--) { | ||
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | ||
count[(arr[i] / exp) % 10]--; | ||
} | ||
for (i = 0; i < n; i++) | ||
arr[i] = output[i]; | ||
} | ||
|
||
void radixsort(int arr[], int n) | ||
{ | ||
int m = getMax(arr, n); | ||
for (int exp = 1; m / exp > 0; exp *= 10) | ||
countSort(arr, n, exp); | ||
} | ||
|
||
void print(int arr[], int n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
cout << arr[i] << " "; | ||
} | ||
int main() | ||
{ | ||
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
radixsort(arr, n); | ||
print(arr, n); | ||
return 0; | ||
} |
Oops, something went wrong.