forked from NITSkmOS/Algorithms
-
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.
selection_sort.cpp: Add cpp selection sort.
Implementation of Cpp selection sort for NITSkmOS/Algorithms. closes NITSkmOS#482
- Loading branch information
1 parent
94e52aa
commit 3bc37ea
Showing
2 changed files
with
32 additions
and
3 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
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,28 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
void selection_sort(int[], int); | ||
|
||
int main() { | ||
int n = 10, arr[] = {5, 7, 9, 8, 3, 1, 4, 2, 10, 6}; | ||
selection_sort(arr, n); | ||
cout << "Sorted array after selection sort algorithm- "; | ||
for(int i = 0; i < n; i++) { | ||
cout << arr[i] << " "; | ||
} | ||
cout << endl; | ||
return 0; | ||
} | ||
|
||
void selection_sort(int arr[], int n) { | ||
//This function implements the main selection sort algorithm. | ||
int i, j, temp; | ||
for(i = 0; i < n; i++) | ||
for(j = i+1; j < n; j++) { | ||
if(arr[i] > arr[j]) { | ||
temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
} | ||
} |