Created
October 11, 2016 06:04
-
-
Save rmsubekti/7ba81aaeb428b0d8fa7d89a040948135 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
int main(int argc, char const *argv[]) { | |
int arr[6] = {2,0,10,5,70,16}, | |
max, | |
min, | |
jumlah = sizeof(arr)/sizeof(arr[0]); | |
std::cout << "Data yang akan di sorting : " << std::endl; | |
for (int i = 0; i < jumlah; i++) { | |
std::cout << "arr[" << i << "] = " << arr[i] << std::endl; | |
} | |
//Selection sort Ascending | |
for (int i = 0; i < jumlah-1; i++) { | |
min = i; | |
for (int j = i+1; j <= jumlah-1; j++) { | |
if (arr[j] < arr[min]) { | |
min =j; | |
} | |
} | |
int temp = arr[min]; | |
arr[min] = arr[i]; | |
arr[i]=temp; | |
} | |
//menampilkan Selection sort Ascending | |
std::cout << "\nData setelah di sorting (Ascending) : " << std::endl; | |
for (int i = 0; i < jumlah; i++) { | |
std::cout << "arr["<< i <<"] = " << arr[i] << std::endl; | |
} | |
//Selection sort Descending | |
for (int i = 0; i <= jumlah-2; i++) { | |
max = i; | |
for (int j = i+1; j <= jumlah-1; j++) { | |
if (arr[j] > arr[max]) { | |
max =j; | |
} | |
} | |
int temp = arr[max]; | |
arr[max] = arr[i]; | |
arr[i]=temp; | |
} | |
//menampilkan Selection sort Descending | |
std::cout << "\nData setelah di sorting (Descending) : " << std::endl; | |
for (int i = 0; i < jumlah; i++) { | |
std::cout << "arr["<< i <<"] = " << arr[i] << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment