/*
Jason M. Snouffer
Computer Science & Programming
Wednesday, December 4, 2002
Lab "11" Problem 3
[Chapter 10 problem 5 (pages 634-636)]
This program utilizes a sorting function, which applies the insertion sort method to sort any sized
vector of integers into ascending order.
Test Suite:
Input (list size): 5
Input (list elements): 5, 4, 3, 2, 1
Expected Output: Your Sorted List:
1
2
3
4
5
Input (list size): 3
Input (list elements): 10, 36, 1
Expected Output: Your Sorted List:
1
10
36
Input (list size): 10
Input (list elements): 12, 4, 89, 10005, -1, 2, 4, 976, 567, 965
Expected Output: Your Sorted List:
-1
2
4
4
12
89
567
965
976
10005
Input (list size): 6
Input (list elements): 56, 7, 464, 3 -10, -15
Expected Output: Your Sorted List:
-15
-10
3
7
56
464
All of the sample inputs within this test suite were tested and performed as expected.
*/
#include
#include
#include
using namespace std;
/*
This function takes a list of integers (vector of ints) and uses insertion sort techniques to sort the list in
ascending order.
precondition: vector must have values
postcondition: vector will be sorted and changed through pass-by-reference
*/
void insertion_sort (vector &list);
/*
This fucntion swaps to elements in a vector of integers
precondition: vector must have values, and element1 & element2 must represent valid integer locations within the vector
postcondition: vector vect will be changed through pass-by-reference by swapping desired elements.
*/
void vector_swap (vector &vect, int element1, int element2);
int main ()
{
vector list;
int num_of_ints;
cout<<"How many integers do you want the list to hold? ";
cin>>num_of_ints;
int i;
for (i = 0; i < num_of_ints; i++)
{
cout<<"Please enter list element #"
<*>vect_element;
list.push_back(vect_element);
}
insertion_sort (list);
cout<<"Your sorted list:\n";
for (i = 0; i < list.size(); i++)
{
cout<* &list)
{
int vector_value;
int i,j;
for (i = 1; i < list.size(); i++)
{
vector_value = list[i];
for (j = i; (j > 0) && (list[j-1] > vector_value); j--)
{
list[j] = list[j-1];
}
list[j] = vector_value;
}
}
/*
This fucntion swaps to elements in a vector of integers
precondition: vector must have values, and element1 & element2 must represent valid integer locations within the vector
postcondition: vector vect will be changed through pass-by-reference by swapping desired elements.
*/
void vector_swap (vector &vect, int element1, int element2)
{
int temp;
temp = vect[element1];
vect[element1] = vect[element2];
vect[element2] = temp;
}