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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* Lab3: working with sorted array
* Author: Luca Del Signore
* Last modified on: Sept 27th, 2014
* Known Bugs: N/A
* Note: Please use assert to check for preconditions */
#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;
/*
Displays the content of an int array, both the array and
the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array
*/
void DisplayArray (int array[], int array_size);
/*
Binary search an array of int for a given value, return true if found, false if not found
the index or would-be index of the value will be set to param index
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array
@param value: the value we are looking for
@param index: upon return, store the index of the occurence of the value (if found),
or the would-be index of the value (if it's not found)
@precondition: array contains array_size number of values in asceoding order
@postcondition: if value appeared in the array, its index is returned; otherwise -1 is returned
*/
int BinarySearch (int array[], int array_size, int value, int & index, int left, int right);
/*
Deletes (i.e., removes) the element stored in the specified position from the SORTED array, the
array should be sorted afterwards
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array
@param index: we want to delete the {index}-th value in the array
@precondition: array contains array_size number of values in asceoding order
index < array_size
@postcondition: if value stored in {index}-th position will be removed, the array is still sorted.
array_size is decreased by 1
*/
void Delete (int array[], int & array_size, int index);
/*
inserts a value into an sorted array so that the array remains sorted,
i.e., the value should be inserted before the first value that is
larger than it.
If the array's content is 2 5 8 10, and the value to be inserted
into the array is 6, it should be put right before 8, and the resulting
array should be 2 5 6 8 10
@param array: the int array that the value is to be inserted into
@param array_size: the current size of the array. Upon successful
insertion, the array_size will be increased by 1
@param value: the value to be inserted
@return: the index of the new value in the array
@precondition: array_size < CAPACITY (i.e., there is space left)
@postcondition: value is inserted into the array, and the array is sorted
array_size is incremented by 1
*/
int InsertByValue (int array[], int & array_size, int value);
/*
insert a value into a given position/index in an array, shifting values
stored at and after that position to the right.
This is a helper function to be called by InsertByValue, and Sort
@param array: the int array that the value is to be inserted into
@param array_size: the current size of the array. Upon successful
insertion, the array_size will be increased by 1
@param value: the value to be inserted
@precondition: index <= array_size and array_size < CAPACITY
@postcondition: value is stored into array[index],
original values stored in array[index], array[index+1], ...
array[array_size-1] is shifted
(i.e., original value in array[index] is now in array[index+1],
original value in array[index+1] is now in array[index+2], ... )
*/
void InsertByIndex (int array[], int array_size, int value, int index);
/*
rearrange the values stored in array into asceoding order
@param array: the int array to be sorted
@param array_size: the size of the array to be sorted
@precondition: none
@postcondition: array[0]<=array[1]<=array[2]<=...<=array[array_size-1] in math sense, not in C++*/
void Sort (int array[], int array_size);
int main()
{
// As the NumArray can be partially filled,
// we use variable NumArraySize to keep track of how many numbers
// have been stored in the array.
int NumArray[CAPACITY]; // an int array with a given capacity
int NumArraySize = 0; // the array is initially empty containing 0 elements
int index = 0;
int value;
// Fill NumArray with data in sorted order
// Display the array afterwards
// Create a sorted array
for (int i = 0; i < 10; i++)
{
NumArray[i] = 3 * (i + 2);
}
NumArraySize = 10;
DisplayArray(NumArray, NumArraySize);
cout << endl;
cout << "Enter a value to search for" << endl;
cin >> value;
BinarySearch(NumArray, NumArraySize, value, index, 0, NumArraySize - 1);
cout << "Enter an index from which an element" << endl;
cout << "of the array will be deleted." << endl;
cin >> index;
Delete(NumArray, NumArraySize, index);
DisplayArray(NumArray, NumArraySize);
}
int BinarySearch (int array[], int array_size, int value, int & index, int left, int right)
{
assert(right > left);
int midpoint = (left + right) / 2;
if (array[midpoint] > value)
{
if (midpoint == left)
{
index = -1;
return -1;
}
return BinarySearch(array, array_size, value, index, left, midpoint);
}
else if (array[midpoint] < value)
{
if (midpoint == left)
{
if (left == right - 1)
{
return array_size - 1;
}
index = -1;
return -1;
}
return BinarySearch(array, array_size, value, index, midpoint, right);
}
else if (array[midpoint] == value)
{
for (int i = left; i <= right; i++)
{
if (array[i] == value)
{
index = i;
return i;
}
}
}
};
void DisplayArray (int array[], int array_size)
{
for (int i = 0; i < array_size; i++)
{
cout << array[i] << endl;
}
};
void Delete (int array[], int array_size, int index)
{
for (int i = index; i < array_size; i++)
{
array[i] = array[i + 1];
}
array_size--;
};
void InsertByIndex (int array[], int & array_size, int value, int index)
{
assert((array_size < CAPACITY) && (index <= array_size));
for (int i = array_size - 1; i >= index; i--)
{
array[i + 1] = array[i];
}
array[index] = value;
array_size++;
};
int InsertByValue(int array[], int & array_size, int value)
{
int desired_index = BinarySearch(NumArray, NumArraySize, value, index, 0, NumArraySize - 1);
InsertByIndex(NumArray, NumArraySize, desired_index, value, desired_index);
};
|