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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void fillArray(int a[], int size, int numberUsed);
void sort(int a[], int numberUsed);
void swapValues(int& v1, int& v2);
int indexOfSmallest(const int a[], int startIndex, int numberUsed);
int main()
{
int sampleArray[10], numberUsed;
fillArray(sampleArray, 10, numberUsed);
sort(sampleArray, numberUsed);
return 0;
}
void fillArray(int a[], int size, int numberUsed) {
int index = 0;
int i, next;
for (i = 0; i < size; i++)
{
next= rand() % 1000 + 10;
numberUsed = next;
}
}
void sort(int a[], int numberUsed) {
int indexOfNextSmallest;
for (int index = 0; index < numberUsed - 1; index++) {
indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
swapValues(a[index], a[indexOfNextSmallest]);
}
}
void swapValues(int& v1, int& v2) {
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int indexOfSmallest(const int a[], int startIndex, int numberUsed) {
int min = a[startIndex],
indexOfMin = startIndex;
for (int index = startIndex + 1; index < numberUsed; index++)
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
}
return indexOfMin;
}
|