Today we learn about sorting and searching algorithm. There are some sorting method algorithm which is divided into two, basic (simple) and intermediate.
Simple sorting method includes bubble sort, selection sort, and insertion sort. Simple sorting has a n^2 of complexity. So, simple sorting is not very effective for large amount of data.
Bubble sort works by comparing two index and move the one with smaller value to the front.
Selection sort works by comparing an index with all other indexes then get the smallest value from all other index then put it on the first index and so on.
Insertion sort works by putting the value of an index to a temporary variable then puts it into the right place, left or right side of the previous indexes.
Intermediate sorting method includes quick sort and merge sort. Intermediate sorting method is better for large amount of data because it has better complexity.
Quick sort uses four different variable to determine the right location for the value of the index then move it there.
Merge sort is based on divide-recur-conquer algorithm. Merge sort works by dividing the data into pairs, then makes the correct order of value by comparing each index on each pairs, then combine the pairs into bigger groups on the correct order and so on.
Searching methods includes linear search, binary search, and interpolation search.
Linear search works by comparing the data with the keyword we’re searching from the first index until the last index. Linear search is not very effective for large amount of data, but can still be used for unsorted data.
Binary search only works if the data is sorted. Binary search works by comparing the middle point of the data ((left + right)/2), then moves the left or right point to produce a new middle point based on the comparison of the middle point’s value and the search keyword. Then repeat the process until the middle point value is the same as the search keyword or until the left point exceed the right point (left > right).
Interpolation search works like binary search but uses different equation for producing the middle point.