Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include <ctime>
- using namespace std;
- void createArray(int arr[], int n);
- void Output(int arr[], int n);
- void SelectionSort(int arr[], int n);
- int main() {
- int n;
- cout << "Day so nguyen co bao nhieu phan tu? (100 / 1000): ";
- cin >> n;
- int* arr = new int[n];
- createArray(arr, n);
- cout << "\nKhoi tao mang " << n << " phan tu: \n";
- Output(arr, n);
- cout << "\n\nSu dung Selection Sort sap xep mang.\nMang sau khi sap xep:";
- SelectionSort(arr, n);
- Output(arr, n);
- return 0;
- }
- void createArray(int arr[], int n)
- {
- srand((int)time(NULL));
- for (int i = 0; i < n;i++) {
- arr[i] = rand() % 201 - 100;
- }
- }
- void Output(int arr[], int n)
- {
- int* count = new int(0);
- for (int i = 0; i < n;i++) {
- if (*count % 10 == 0)
- cout << endl;
- cout << setw(5) << arr[i];
- (*count)++;
- }
- delete count;
- }
- void SelectionSort(int arr[], int n)
- {
- int min;
- for (int i = 0; i < n - 1;i++) {
- min = i;
- for (int j = i + 1;j < n;j++) {
- if (arr[j] < arr[min])
- min = j;
- }
- if (min != i)
- swap(arr[min], arr[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement