DSA Sort Alg

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

DSA REVIEW: CODE SECTION

I. Sorting Algorithms
Time Complexity Space
Stability
Algorithms Summary (Case) Complexity
Worst Best
Selects the smallest element from an unsorted list in each
Selection Sort iteration and places that element at the beginning of the O(n2) O(n2) O(1) No
unsorted list.
Places an unsorted element at its suitable place in each
Insertion Sort O(n2) O(n) O(1) Yes
iteration.
Compares two adjacent elements and swaps them if they are
Bubble Sort O(n2) O(n) O(1) Yes
not in the intended order.
Quick Sort Divide and Conquer approach O(n2) O(nlogn) O(logn) No
Merge Sort Divide and Conquer algorithm O(nlogn)) O(nlogn) O(n) Yes
Visualizing the elements of the array as a special kind of
Heap Sort O(nlogn) O(nlogn) O(1) No
complete binary tree called a heap.
Sorts the elements of an array by counting the number of
Counting occurrences of each unique element in the array. The count is
stored in an auxiliary array and the sorting is done by O(n+k) O(n+k) O(max) Yes
Sort
mapping the count as an index of the auxiliary array.

Linear Sorts the elements by first grouping the individual digits of


Time Radix the same place value. Then, sort the elements according to
O(n+k) O(n+k) O(max) Yes
Sorting Sort their increasing/decreasing order.

Divides the unsorted array elements into several groups


Bucket called buckets. Each bucket is then sorted by using any of the
O(n2) O(n+k) O(n+k) Yes
Sort suitable sorting algorithms or recursively applying the same
bucket algorithm.
Best Sorting Algorithm
• Linear time sorting algorithm:
 Fastest, but need special input data
• The fastest comparison sorting algorithms has O(N.logN)
1. For large N(say>1000), Quicksort is faster, on most machines, by a factor of 1.5 or 2.
However, it requires a bit of extra memory and is a moderately complicated program.
2. Heap sort is a true “sort in place”, and is more compact to program.
3. For N<50, an O(N2) sorting algorithm, is concise and fast enough.
4. For 50<N<1000, Shell sort is usually the method of choice (see section 7.4 text book).
It is O(N3/2) as in the worst case, but is usually faster.
II. Search Algorithms
Algorithm Code Steps to find k in array [x]
1. From the first element, compare k with each
LinearSearch(array, key) element x
2. If x==k, return the index.
for each item in the array 3. Else, return not found
Linear Search
if item == value -> {return its index}
1. Rearrange the array to ascending order.
do until the pointers low and high meet each other. 2. Set 2 pointers low and high at the lowest
mid = (low + high)/2 and the highest positions respectively.
3. Find the middle element (mid) of the
Iterative if (x == arr[mid]) -> {return mid} array.
Method
arr[(low+high)/2]
else if (x > arr[mid]) -> {low = mid + 1}
4. If x==mid, then return mid, else compare
else -> {high = mid – 1} the element to be searched with m
 x>mid, compare x with the middle
element of the elements on the right
binarySearch(arr, x, low, high) side of mid. This is done by setting
low to low=mid+1
Binary if low > high -> {return False}
Search  else (x<mid), compare x with the
else { middle element of the elements on
the left side of mid. This is done by
mid = (low + high) / 2 setting high to high=mid-1.
Recursive
Method if x == arr[mid] -> {return mid} 5. Repeat steps 3 to 6 util low meets high.
(Follow 6. Found
Divide&Conquer) else if x > arr[mid]

-> {return binarySearch(arr, x, mid + 1, high)}

else -> {return binarySearch(arr, x, low, mid - 1)}

You might also like