# 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Make two pointers called start and end set to the start index, and the end index.
Loop throug the array, if the value at current index is red, swap it with start and increment both,
if the value at current index is blue, swap with last end index, decrement end index and increment current index.
If its white, increment the current index
|
|
/ basically in javascript you store the heap in an array.
// the way you do this is by following this rule
// left child: i * 2
// right child: i * 2 + 1
// parent: i / 2
// to insert you add a new value to the end of the array, check if its smaller than its parent, if so then you move it up by swapping it with the parent, then check again.
//Each time you need to check to see if you have reached the first index of the array. Or if the next value is the first index of the array, if so stop. Else make the current index which is the child, the index of the parent.
// to remove its more complicated. but simlar concept.
//you are going to remove the root because its always the smallest. Remove it, and swap the very last element of the array with the very first.
//if the length is 2, remove the first value, and move the seoncd into the first place.
//if the length is 3, swap the new root with the smaller of the children you are good
//else, use a while loop. use the formula to get the right and left children,
// while the value at the index is greater than either of its children, swap it with its smaller child. Then set the index to that of the child.
//lastly, if either of the new children are undefined, then break. Else continue the loop.
// lastly return the smallest value you removed at the beginning. |
|
These last 3 days Im not going to do.
I need to focus on finishing up preparation for my teaching class, and then ill come back to this and start a new commitment or something. |
|
I actually did not yesterday but I was too buzy so im gonna give myself a pass |
|
3809355380791738073493805374