1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
void selectionSort(int arr[], int size);
void swap(int& x, int& y);
int main()
{
int numbers[] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15, 234 };
int maximum = sizeof( numbers ) / sizeof( numbers[0] );
int k;
selectionSort(numbers, maximum);
ofstream output;
output.open("sorted.txt");
for (k = 0; k < maximum; k++)
output << numbers[k] << "\n";
output.close();
system("sorted.txt");
return 0;
}
void selectionSort(int arr[], int size)
{
int indexOfMin, pass, j;
for (pass = 0; pass < size - 1; pass++)
{
indexOfMin = pass;
for (j = pass + 1; j < size; j++)
if (arr[j] < arr[indexOfMin])
indexOfMin = j;
swap(arr[pass], arr[indexOfMin]);
}
}
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
|