Sort and Search
Implementing a sort and search algorithm.
This wasn't quite difficult but when I write code that I've never done it before my brain isn't quite sure which way to go about doing it. This problem set involved using someone elses code, and then adding to it. Through this example I can begin to see how a program can be broken up to work within a team of people.
Once I see an example snippet, I kinda go ah, that is how you do it, and then begin to implement it in my own way, and it wasn't difficult to see how to code the search and sort functions.
Sort
I wrote a selection sort algorithm for this problem.
The code is relatively simple. You use 1 loop to iterate through the array of numbers. It essentially keeps track of the first unsorted number.
The second loop basically scans the rest of the array for the remaining smallest number and then swaps in that number.
The first loop iterates to the 2nd number, then finds the next smallest, swaps, and so forth until it reaches the end of the array, and that means the array will then be sorted from smallest to largest.
----------------
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
for (int i=0; i < n-1 ; i++)
{
int min = i;
//finds index value of min
for (int j = i+1; j < n ; j++)
{
//finds lowest number between 2 integers
if (values[j] < values[min])
{
min = j;
}
//swaps array integers
if (min != i)
{
int high = values[i];
values[i] = values[min];
values[min] = high;
}
}
}
return ;
}
Search
I learned a lot of about algorithms and the scaleability time they take to function during PSET 3.
searching an array of 5 numbers [2,6,8,13,15] it's easy to search 1 by 1. However if the array was 13 trillion numbers, this would take forevor. A better approach would be to continually split the problem in half until the number is found.
This does require the array to be sorted first. This approach won't work unless the list is sorted.
bool search(int value, int values[], int n)
{
//find start, end and middle of the value array
int start = 0;
int end = n-1;
//searches for needle using binary search
while (start <= end )
{
int middle = (start+end) / 2;
if (value == values[middle] )
{
return true;
}
else if ( value > values[middle])
{
start = middle + 1;
}
else if ( value < values[middle])
{
end = middle-1;
}
else
break;
}
return false;
}
The Game of Fifteen
I'll have 13 days to complete this before the PSETS's change in the new year. It looks like it may involve swapping multidimensional arrays which will be similar"ish" to the sort function.
I'll probably start the problem today and try to write down some pseudocode. The example files have 200 lines of code, and this will be the largest program i have worked with to date, and feels a little intimidating at first.
Edit:
This may be difficult. The basic game structure is given, I just have to code some functions to make the game work. I't's difficult to visualize, as I don't understand how the code fits together, or how the game works.
No comments:
Post a Comment