-
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.
- Loading branch information
1 parent
b812418
commit 8896035
Showing
2 changed files
with
97 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,25 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
void insertion_sort(int arr[],int n){ | ||
for (int i=1;i<n;i++){ | ||
int j=i; | ||
while (j > 0 && arr[j - 1] > arr[j]) { | ||
int temp = arr[j - 1]; | ||
arr[j - 1] = arr[j]; | ||
arr[j] = temp; | ||
j--; | ||
} | ||
} | ||
}; | ||
int main(){ | ||
int n; | ||
cin>>n; | ||
int arr[n]; | ||
for(int i=0;i<n;i++) cin>>arr[i]; | ||
insertion_sort(arr,n); | ||
for(int i=0;i<n;i++){ | ||
cout<< arr[i] <<" "; | ||
} | ||
cout << endl; | ||
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,72 @@ | ||
#include<bits/stdc++.h> | ||
|
||
using namespace std; | ||
// void selection_sort(int arr[], int n) { | ||
// // selection sort | ||
// for (int i = 0; i < n - 1; i++) { | ||
// int mini = i; | ||
// for (int j = i + 1; j < n; j++) { | ||
// if (arr[j] < arr[mini]) { | ||
// mini = j; | ||
// } | ||
// } | ||
// int temp = arr[mini]; | ||
// arr[mini] = arr[i]; | ||
// arr[i] = temp; | ||
// } | ||
|
||
void bubble_sort(int arr[], int n) { | ||
for (int i = n - 1; i > 0; i--) { | ||
int didswap =0; | ||
for (int j = 0; j < i; j++) { | ||
if (arr[j] > arr[j + 1]) { | ||
// Swap arr[j] and arr[j+1] | ||
int temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
didswap=1; | ||
} | ||
} | ||
if(didswap == 0){ | ||
break; | ||
} | ||
cout<<"runs"; | ||
} | ||
} | ||
|
||
|
||
// cout << "After selection sort: " << "\n"; | ||
// for (int i = 0; i < n; i++) { | ||
// cout << arr[i] << " "; | ||
// } | ||
// cout << "\n"; | ||
|
||
// } | ||
//int main() { | ||
// int arr[] = {13,45,22,50,20,9}; | ||
// int n = sizeof(arr) / sizeof(arr[0]); | ||
// cout << "Before selection sort: " << "\n"; | ||
// for (int i = 0; i < n; i++) { | ||
// cout << arr[i] << " "; | ||
// } | ||
// cout << "\n"; | ||
// selection_sort(arr, n); | ||
// return 0; | ||
// } | ||
|
||
int main() { | ||
int n; | ||
cin >> n; | ||
int arr[n]; | ||
for (int i = 0; i < n; i++) | ||
cin >> arr[i]; | ||
|
||
bubble_sort(arr, n); | ||
|
||
for (int i = 0; i < n; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
return 0; | ||
} |