AlgoDS/Algorithm

Searching and Sorting with Array

데먕 2019. 9. 4. 21:12

1. Overview

Two array processing technique that are particularly common are searching and sorting. Searching here refers to finding an item in the array that meets some specified criterion. Sorting refers to rearranging all the items in the array into increasing or decreasing order.

 

2. Description

Sorting Process Big O
Selection Sort
  1. Find the smallest value in A and put it in A[0]
  2. Find the second smallest value in A and put it in A[1]
  3. and so on(smallest or largest, depending on sorting order)
  • always O(N^2)
Insertion Sort
  1. Put the first 2 items in a correct relative order
  2. Insert the 3rd in the correct place relative to the first 2
  3. Insert the 4th item in the correct place relative to the first 3
  4. and so on
  • O(N) on an already-sorted array
  • O(N^2) on worst-case
Merge Sort
  1. Recursively sort the first N/2 items
  2. Recursively sort the last N/2 items
  3. Merge using an auxiliary array
  • always O(N*log N)
Quick Sort
  1. Choose a pivot value
  2. partition the array
    1. Left part has item <= pivot
    2. Right part has item >= pivot
  3. Recursively sort the left part
  4. Recursively sort the right part
  • O(N^2) on worst-case
  • O(N*log N) on expected

 

  • Selection Sort

  • Insertion Sort

 

  • Merge Sort

  • Quick Sort

 

3. References

http://math.hws.edu/javanotes/c7/s4.html

http://pages.cs.wisc.edu/~bobh/367/SORTING.html

https://runestone.academy/runestone/books/published/pythonds/SortSearch/TheSelectionSort.html

https://www.techiedelight.com/merge-sort/

https://codesjava.com/java-quick-sort-algorithm-example