Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void swap(int &a, int &b) {
- int temp = a;
- a = b;
- b = temp;
- }
- void SelectionSort(int arr[], int n) {
- int current = 0;
- while (current < n - 1) {
- int smallest = current;
- int walker = current + 1;
- while (walker < n) {
- if (arr[walker] < arr[smallest])
- smallest = walker;
- walker++;
- }
- swap(arr[current], arr[smallest]);
- current++;
- }
- }
- int main(){
- const int n = 6;
- int arr[n] = { 23,78,45,8,32,56 };
- SelectionSort(arr, n);
- for (int i = 0; i < n; i++) {
- cout << arr[i] << " ";
- }
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement