-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.cpp
72 lines (63 loc) · 1.51 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
65
66
67
68
69
70
71
72
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
class Sorting{
public:
//bubble sort
void bubbleSort(int nums[],int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(nums[j]>nums[j+1]){
int temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
}
//insertion sort
void insertionSort(int nums[],int n){
for(int i=0;i<n-1;i++){
for(int j=i+1;j>0;j--){
if(nums[j]<nums[j-1]){
int temp=nums[j];
nums[j]=nums[j-1];
nums[j-1]=temp;
}
}
}
}
//selection sort
void selecionSort(int nums[],int n){
for(int i=0;i<n;i++){
int max=findMax(nums,i,n);
//lets swap the curr max with the last element
int temp=nums[max];
nums[max]=nums[n-i-1];
nums[n-i-1]=temp;
}
}
private:
int findMax(int nums[],int idx,int size){
int maxx=0;
for(int i=0;i<=size-idx-1;i++){
if(nums[i]>nums[maxx]) maxx=i;
}
return maxx;
}
};
int main(){
Sorting s;
int * arr=new int[10];
//int arr[]={4,3,2,1};
int N=10;
for(int i=0;i<N;i++){
arr[i]=rand()%N;
}
s.bubbleSort(arr,N);
for(int i=0;i<N;i++){
cout<<arr[i]<<" ";
}
return 0;
}