-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAll_sorting.cpp
More file actions
59 lines (53 loc) · 1.13 KB
/
Copy pathAll_sorting.cpp
File metadata and controls
59 lines (53 loc) · 1.13 KB
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
#include <iostream>
#include <vector>
using namespace std;
// Printing Array
void print_arr(const vector<int>&arr){
for (int i = 0;i<arr.size();i++){
cout<<arr[i];
}
cout <<endl;
}
//Taking Input
void input_arr(const vector<int>&arr){
vector<int> arr(arr.size());
for (int i =0; i<arr.size();i++){
cin >> arr[i];
}
}
// Code Bubble Sort
void bubble_sort(int arr[],n){
bool swapped;
for (int i= 0;i<n-1;i++){
swapped = false;
for (int j=0;j<n-i-1;j++){
if (arr[j]<arr[j+1]){
swap(arr[j],arr[j+1]);
swapped = true;
}
}
if (swapped ==false){
break;
}
}
}
// Selection Sorting
void select_sort(vector<int>& arr){
for(int i=0; i<arr.size();i++){
int min_index = i;
for (int j=i+1;j<arr.size();j++){
if(arr[j]<arr[min_index]){
min_index = j;
}
}
swap(arr[i],arr[min_index]);
}
print_arr(arr);
}
// Main Function
int main(){
int n ;
cout <<"Enter how many indexes you want: ";
cin >> n;
input_arr(n);
}