QuickSort. We start by choosing a pivot point as a reference and we pull the items less than the pivot out and also the items greater than the pivot and create a two lists containing them separately. Then we then we quick-sort these two lists and finally by recursive, thus our list is sorted. Notice every time we get these two featured lists, the final position of our pivot is found. Thus every time we cut our list roughly into two halves according to the value of our pivot. We shouldn't assume that our pivot is the first item of our list, it can be random, which also helps save time.
MergeSort. We start by continuing cutting our list in to two halves until we reach the base case, i.e. length of list is 1. Then here's a significant function called merge, which takes two smaller lists and combines them together into a single, sorted list. In this way we get our list sorted properly. But it requires much more spaces since our list is cut into n slides.
MergeSort. We start by continuing cutting our list in to two halves until we reach the base case, i.e. length of list is 1. Then here's a significant function called merge, which takes two smaller lists and combines them together into a single, sorted list. In this way we get our list sorted properly. But it requires much more spaces since our list is cut into n slides.
No comments:
Post a Comment