Chapter 5.
SORTING
[Link] is sorting?
• Sorting is the process of ordering and arranging a given collection of elements in some particular
order.
• We can sort a collection of elements in ascending or descending order.
• if it is collection of string we can sort it in alphabetical order.
2. what is bubble shot?
• it sort the given list of elements repeatedly comparing the adjacent element and swapping with
them if they are unordered.
• swapping 2 elements means changing the position with each other.
• in algorithm every interaction through each elements of list is called pass.
• for a list of n elements, the bubble shot make a total of N -1 passes to short the list.
• in order to arrange the element in ascending order, the largest element is identified after each
pass and place the correct position in the list.
[Link] is selection sort?
• The list is divided into sorted and unsorted parts.
• The left list contain sorted elements, right list contain unsorted element.
• the left list is empty, right list contains all elements.
• The smallest element from the unsorted list is selected and swapped with the first unsorted
element.
• To sort a list having N elements, The selection sort makes n-1 number of passes through the list.
4. what is insertion sort?
• Insertion sort is a sorting method that organizes a list of elements in ascending or descending
order.
• The list is divided into two parts: a sorted part and an unsorted part.
• It takes each element from the unsorted part one by one and inserts it into the correct position in
the sorted part.
• In each pass, sorted list is traversed from the backward direction to find the position where the
unsorted element could be inserted.
• Hence, this sorting method is called insertion sort.
5. What is time complexity of algorithm?
a) The amount of time an algorithm take to process a given data.
b) The following tips will guide for estimating the time complexity of an algorithm.
• Any algorithm that does not contain any loop will have time complexity as 1.
Since the number of instructions to be executive will be constant.
• Any algorithm that has a loop will out the time complexity as n.
Because the loop will execute the statement inside the body n number of times.
• Any algorithm within a loop will have time complexity as n².
Such algorithm are known as quadratic time algorithm.