30 likes | 160 Views
MergeSort. O( n log n ), Ω ( n log n ) Sort (list of n ) If n <2, return. Else Sort (left half). Sort (right half). Merge sorted halves. int main (void) { int ans = factR(5); }. int fact(5) factR (int numR) { //base case! if (numR <=1)
E N D
MergeSort • O(n log n), Ω(n log n) Sort (list of n) Ifn<2,return. Else Sort(lefthalf). Sort(righthalf). Mergesortedhalves.
int main (void) { int ans = factR(5); } int fact(5) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } How it happens int fact(4) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } int fact(3) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } int fact(2) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } int fact(1) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } int fact(0) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); }
int main (void) { int ans = factR(5); return 0; } int fact(5) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } How it happens 1 ans = 120! int fact(4) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } 1 int fact(3) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } 2 int fact(2) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } int fact(1) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } 6 ( 24 ) int fact(0) factR (int numR) { //base case! if (numR <=1) return 1; else return numR * factR(numR-1); } ( 6 ) 24 ( 2 ) ( 1 ) 120 ( 1 )