Code:
////////////////////////////////////////////////////////////////////////////////
//
// Author:
// name: #######, ID Number: #######
// Year: 2008, Course: #######
//
// Program Name:
// Description:
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
//constant size N used on arrays
const int N = 10;
//Prints author details
void detials (void);
/*printArray prints the values of the array sorted by ether the
insSortItr fuction or the insSortRec function*/
void printArray (int dat[], int n);
/*insSorItr sorts data in ascending order of the array
[a[first]...[last]] using itteration (while and for loops).*/
void insSortItr (int a[], int first, int last);
/*insSortRec sorts data in ascending order of the array
[a[first]...[last]]using recursion.*/
void insSortRec (int b[], int c[], int index, int len);
int main (){
// array of numbers to sort for the insSortItr function
int dataA[N] = {26,5,26,1,61,11,59,15,11,19};
/* array of unsorted numbers that insSortRec function will read
and sort into final array */
int dataB[N] = {26,5,26,1,61,11,59,15,11,19};
/*final will be the final sorted array of numbers from dataB unsortted
array, they will be sorted by the recursive insSortRec insertion sort
function and stored*/
int final[N];
/*final will be the final sorted array of numbers from dataB unsortted
array, they will be sorted by the recursive insSortRec insertion sort
function and stored*/
detials ();
insSortItr (dataA,0,N);
cout << "After iterative insertion sort finished:\n";
printArray (dataA,N);
cout << "\n\n";
cout << "Part B: Initial array:\n";
printArray (dataB,N);
cout << "\n\n";
insSortRec (final,dataB,0,N);
//cout << "After recursive insertion sort finished:\n";
//printArray (final,N);
//end - clearscreen
}
//////////////////////////////////////////////////////////////////////
//////////////// voids functions
//////////////////////////////////////////////////////////////////////
//A fuction that displaying all Author names and Ids
void detials(void){
cout << "159.201 Assingment 1\n";
cout << "Smith C, ID:07199104\n\n";
}
/* PrintArray function prints the array values supplied to it
when calling it at any time*/
void printArray (int dat[], int n){
for (int k = 0; k < n; ++k){
cout << dat[k] << " ";
}
}
/*iterative insertion sort - insSortItr takes array input value
iterates through the loop preforming insertion sort algorithum
and printing values out at each itteration using the printArray
function*/
void insSortItr (int a[], int first, int last){
cout << "Part A: Initial array:\n";
printArray(a,last);
cout << "\n\n";
for (int j = first+1 ; j <=last ; ++j){
int kti = a[j];
int i = j-1;
//cout << a[1] << "\n";
while (i >= first && a[i] > kti){
a[i+1] = a[i];
i = i-1;
a[i+1] = kti;
}
printArray(a,last); // displays content of a each iteration
cout << "\n";
}
cout << "\n";
}
//recursive insertion sort - insSortRec
//display all intermediate results
void insSortRec (int b[], int c[], int index, int len){
int temp = c[index];
/* need to figure out how to insert contence in temp
into the final array then display it like insSortItr*/
if(index < len -1){
index++;
insSortRec(b,c,index,len); // This is the recursive part
}
//printArray (c,N);
//cout << "\n";
}