Binary Insertion Sort

I'm feeling very stuck with what to do, very new at this. I'm supposed to write an insertion sort and modify it such that instead of using a backwards linear search to find the location that the insertion elements belongs, I will use a binary search. The number of comparisons will be tracked in the SortTester to see if my comparisons are in the range that indicates I have adjusted the search.



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//main.cpp

#include <fstream>
#include <iostream>
#include <time.h>

#include "SortTester.h"

using namespace std;

typedef unsigned int uint;

uint findInsertionLocation(SortTester &tester, uint start, uint end, uint index) {
   
//insert code to find the location to insert the next element into


return 0;

}

void insertionSort(SortTester &tester, uint size) {

//insert code for insertion sort here
//HINT//
//First get your insertion sort working by just searching backward for location
//Then once that is working - work on implmenting the binary search for the insertion location
}

int main() {
	uint size = 10;
	SortTester tester = SortTester(size);
	cout<<"Unsorted"<<endl;
	tester.print();
	insertionSort(tester, size);
	if (tester.isSorted()) {
		cout<<"PASSED List Sorted (10 pts)"<<endl;
	}
	else {
		tester.print();
		cout<<"FAILED List not Sorted"<<endl;
	}

	if (tester.areComparesBinary()) {
	   cout<<"PASSED Binary Insertion Sort (15 pts)"<<endl;
	}
	else {
	   cout<<"FAILED Binary Insertion Sort"<<endl;
	}

	if (tester.isSortStable()) {
	   cout<<"PASSED Extra Credit - sort is stable (5pts)"<<endl;
	}
	else {
	   cout<<"Sort is not stable - swap occured among entries with same value"<<endl;
	}
}



//SortTester.h

#include <fstream>
#include <vector>


class SortTester {
    private:
		std::vector<int> testList;
		unsigned int numCompares;
		bool stableSort;

    public:
        SortTester(unsigned int numEntries);
        void swap(unsigned int a, unsigned int b);
        //returns positive number if a > b, 0 if a==b, and negative number if a < b
        int compare(unsigned int a, unsigned int b);
        void print();

        bool areComparesBinary();
        bool isSorted();
        bool isSortStable();
};


//SortTester.cpp

#include <iostream>
#include <time.h>
#include <cmath>

#include "SortTester.h"

using namespace std;

SortTester::SortTester(unsigned int numEntries) {
	numCompares = 0;
	stableSort = true;
	srand(time(NULL));
	for (unsigned int i=0; i < numEntries; i++ ) {
		testList.push_back( rand() % 100);
	}
}

void SortTester::print() {
	for (auto & it : testList) {
		cout<<it<<" ";
	}
	cout<<"\n";
}

int SortTester::compare(unsigned int a, unsigned int b) {
	numCompares++;
	return testList[a] - testList[b];
}

void SortTester::swap(unsigned int a, unsigned int b) {
	if ((testList[a] == testList[b]) and (a != b)) {
		stableSort= false;
	}
	if ( (a > testList.size()) or (b > testList.size()) or (a<0) or (b<0) ) {
		cout<<"Invalid swap request of "<<a<<" and "<<b<<" no swap performed"<<endl;
		return;
	}
	int temp = testList[a];
	testList[a] = testList[b];
	testList[b] = temp;
	return;
}

bool SortTester::isSorted() {
	bool sorted = true;
	for (unsigned int i=0; i < testList.size() - 1; i++) {
		if (testList[i] > testList[i+1] ) {
			sorted = false;
		}
	}
	if ( sorted ) {
		return true;
	} //implied else
	print();
	return false;
}

bool SortTester::areComparesBinary() {
	unsigned int n = testList.size();
	return numCompares < 2*n*log2(n);
}
bool SortTester::isSortStable() {
	return stableSort;
}

where is the insertion sort?
if you have not done that, did you have a question on it? Do what they said, write it the normal way first, but make the search loop part a function so you can swap it out with the binary search after.
https://en.wikipedia.org/wiki/Insertion_sort

insertion sort leads to shell sort which is much, much more interesting IMHO.
Last edited on
Topic archived. No new replies allowed.