0% found this document useful (0 votes)
3 views1 page

Selection Sort

The document is a C++ program that implements the Selection Sort algorithm to sort an array of integers in ascending order. It prompts the user to input the number of data elements and their values, then sorts the array using the selection sort method. Finally, it outputs the sorted array.

Uploaded by

mikailmahawira
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Selection Sort

The document is a C++ program that implements the Selection Sort algorithm to sort an array of integers in ascending order. It prompts the user to input the number of data elements and their values, then sorts the array using the selection sort method. Finally, it outputs the sorted array.

Uploaded by

mikailmahawira
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SELECTION SORT

#include <iostream>
using namespace std;

int main() {
int n;
cout << "Masukkan jumlah data: ";
cin >> n;

int data[n];

// Input data
for (int i = 0; i < n; i++) {
cout << "Masukkan data ke-" << i + 1 << ": ";
cin >> data[i];
}

// Proses sorting (Selection Sort - Ascending)


for (int i = 0; i < n - 1; i++) {
int indexMin = i;

for (int j = i + 1; j < n; j++) {


if (data[j] < data[indexMin]) {
indexMin = j;
}
}

// Tukar elemen
int temp = data[i];
data[i] = data[indexMin];
data[indexMin] = temp;
}

// Output hasil sorting


cout << "\nData setelah diurutkan (Ascending): ";
for (int i = 0; i < n; i++) {
cout << data[i] << " ";
}

return 0;
}

You might also like