-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sorting.cpp
64 lines (60 loc) · 1.11 KB
/
Sorting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <climits>
#define MAXN 100
using namespace std;
void BubbleSort(int arr[], int n){
for(int j=0;j<n-1;j++){
for(int i=0;i<n-1-j;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
void SelectionSort(int arr[], int n){
int i=0;
while(i<n){
int minIndex = -1;
int min = INT_MAX;
for(int j=i;j<n;j++){
if(arr[j]<min){
minIndex=j;
min=arr[j];
}
}
if(minIndex!=i){
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex]=temp;
}
i++;
}
}
void CountingSort(int arr[], int n){
int aux[MAXN] = {0};
for(int i=0;i<n;i++){
aux[arr[i]]+=1;
}
int k = 0;
for(int i=0;i<MAXN;i++){
int c = aux[i];
for(int j=0;j<c;j++){
arr[k] = i;
k+=1;
}
}
}
int main(){
int n;
cin >> n;
int arr[MAXN];
for(int i=0;i<n;i++){
cin >> arr[i];
}
CountingSort(arr,n);
for(int i=0;i<n;i++){
cout << arr[i] << " ";
}
}