Last active
August 29, 2015 14:14
-
-
Save LouisJenkinsCS/ca06d99f68db76b02bf5 to your computer and use it in GitHub Desktop.
Attempt at making a Merge Sort in pseudocode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A[] = { 28, 39, 48, 27, 9, 88, 11, 4, 7} // Global variable, disregard bad programming practices | |
Function Split(int low, int hi){ // Adding brackets because not only am I used to java, it should help readability | |
if (hi - low) < 2 then | |
return array of elements from [low, hi] | |
mid = (hi + low) / 2 | |
B[] = split(low, mid-1) // Either I do mid-1 or mid+1, the results seem the same | |
C[] = split(mid, hi) // Same problems as above | |
D[] = Merge(B[], C[]) | |
return D[] | |
End Function | |
Function Merge(B[], C[]) // I use two arrays because I figured it'd be the easiest to work with. | |
int PosB = 0, PosC = 0, PosMax = (b.length + c.length) -1 // PosB and PosC keep track of the positions in the cells of B and C | |
// respectively. PosMax keeps track of the max cell that E[] (declared below) should have as well as the max number for the for loop | |
E[num] // Declare and iniatialize an array that is supposed to hold the combined values of B and C, sorted. | |
for i = 0 to num | |
if PosC >= c.length or B[PosB] < C[PosC] // Checks if C[] already has added everything it has to E[] and if so, proceeds to add the | |
// supposedly already sorted array B[]'s elements to E[]. Emphasis on "supposedly". A problem here is it does not check for if PosB | |
// is out of bounds, which is a huge problem with primitive arrays. Also checks if the current element in C is greater than the | |
// current element in B, and if so, add the element in B and increment. | |
E[i] = B[PosB] | |
PosB += 1 // Does not check whether or not PosB is at the end, gotta figure a way to check | |
Else | |
E[i] = C[PosC] | |
PosC += 1 | |
End If | |
Next | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment