-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
73 lines (61 loc) · 2.24 KB
/
selectionSort.cpp
File metadata and controls
73 lines (61 loc) · 2.24 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
// Listing 11-1.
#include <iostream>
#include <string>
using namespace std;
/** Finds the largest item in an array.
@pre The size of the array is >= 1.
@post The arguments are unchanged.
@param theArray The given array.
@param size The number of elements in theArray.
@return The index of the largest entry in the array. */
/** Sorts the items in an array into ascending order.
@pre None.
@post The array is sorted into ascending order; the size of the array
is unchanged.
@param theArray The array to sort.
@param n The size of theArray. */
template <class ItemType>
int findIndexofLargest(const ItemType* theArray, unsigned int size)
{
int indexSoFar = 0; // Index of largest entry found so far
for (int currentIndex = 1; currentIndex < size; currentIndex++)
{
// At this point, theArray[indexSoFar] >= all entries in
// theArray[0..currentIndex - 1]
if (theArray[currentIndex] > theArray[indexSoFar])
indexSoFar = currentIndex;
} // end for
return indexSoFar; // Index of largest entry
} // end findIndexofLargest
template <class ItemType>
void selectionSort(ItemType* theArray, unsigned int n)
{
// last = index of the last item in the subarray of items yet
// to be sorted;
// largest = index of the largest item found
for (int last = n - 1; last >= 1; last--)
{
// At this point, theArray[last+1..n-1] is sorted, and its
// entries are greater than those in theArray[0..last].
// Select the largest entry in theArray[0..last]
int largest = findIndexofLargest(theArray, last+1);
// Swap the largest entry, theArray[largest], with
// theArray[last]
std::swap(theArray[largest], theArray[last]);
cout << "Pass " << n-1 - last << " gives: " << endl;
for (int i = 0; i < n; i++)
cout << theArray[i] << " ";
cout << endl;
} // end for
} // end selectionSort
int main()
{
string a[11] = {"6", "3", "9", "8", "8", "3", "1", "7", "3", "9", "1"};
selectionSort(a, 11);
cout << "After sorting " << endl;
for (int i = 0; i < 11; i++)
cout << a[i] << " ";
cout << endl;
} // end main