recursionNotes.m - Sorting INSERTION SORT BUBBLE SORT O(N^2 comparing a pair of values to see if they are in order[3 42 pi-6 24 85-50 >[3 pi-6 24 42

recursionNotes.m - Sorting INSERTION SORT BUBBLE SORT O(N^2...

This preview shows page 1 - 2 out of 2 pages.

%% Sorting------------------------------------- %% INSERTION SORT %{ %} %% BUBBLE SORT %{ O(N^2) comparing a pair of values to see if they are in order [3 42 pi -6 24 85 -50] > [3 pi -6 24 42 85 -50] > [3 -6 pi 24 -50 42 85] > [-6 3 pi -50 24 42 85] > [-6 3 -50 pi 24 42 85] > [-6 -50 3 pi 24 42 85] > [-60 -6 3 pi 24 42 85] for r = 1:N for c = 2: N + 1 - r if v(r) > v(c) swap(v(r),v(c)) end end end %} %% MERGE SORT %{ O(N*logN) Take 2 vectors that are already sorted and create 1 vector (recursive) [42 pi -6 100 72 87 21 0 1] [42][pi][-6][100][72][87][21][0][1] [pi 42] [-6 pi 42][72 100][72 100][21 87][0 1] [-6 pi 42 72 100][0 1 21 87] [-6 0 1 pi 21 42 72 87 100] fn mergeSort(v) split in half mergeSort(left) mergeSort(right) merge(left,right) end %} %% QUICK SORT %{ O(N*logN), O(N^2)
Image of page 1
Not a better big O, but increases speed by 3 [50 25 75 17 21 82 11 100 98] pick a pivot (50)
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Spring '08
  • Stallworth

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors