2. • Arranging the data in a logical order
•
• Ascending order : is a method of arranging numbers from
smallest value to largest value.
• OR
• Descending order: is a method of arranging numbers from
largest value to smallest value.
3. Realtime applications:
1. While shopping on flipkart or amazon,
you sort items based on your choice, that is,
price low to high or high to low
2. The contact list in your phone is sorted, which means you can
easily access your desired contact from your phone since the
data is arranged in that manner for you. In other words, “it is
sorted”.
5. Insertion sort
• Logic: The array is considered into two parts
• Sorted and Unsorted.
• Initially, the sorted array contains zero (0) elements, and the unsorted
array contains n elements.
• Assume the first element (0th index) is sorted.
• Now sorted array contains 1 element, and unsorted contains n-1
elements
• Now consider the first element present in the unsorted and insert it
into the sorted array at the appropriate position (called insert into
position)
• Repeat the process until all the elements present in the array are in
sorted order.
• https://www.youtube.com/watch?v=OGzPmgsI-pQ
6. • Initial list: [5, 2, 4, 6, 1, 3]
• Start with the second element (2). Compare it with the first element
(5) and swap them since 2 is smaller. Updated list: [2, 5, 4, 6, 1, 3]
• Move to the third element (4) and compare it with the previous
elements (5, 2). Swap 4 with 5 and then with 2 to place it in the
correct position within the sorted portion. Updated list: [2, 4, 5, 6, 1,
3]
• Move to the fourth element (6). It's already greater than the previous
elements, so no swaps are necessary. Updated list: [2, 4, 5, 6, 1, 3]
• Move to the fifth element (1) and compare it with the previous
elements (6, 5, 4, 2). Swap it with each previous element until it
reaches its correct position. Updated list: [1, 2, 4, 5, 6, 3]
• Move to the last element (3) and compare it with the previous
elements (6, 5, 4, 2, 1). Swap it with each previous element until it
reaches its correct position. Updated list: [1, 2, 3, 4, 5, 6]
7. Selection sort:
• Logic: The array is considered into two parts
• unsorted and sorted
• initially the whole array is unsorted
• Step 1: Select the lowest element present in the array
• Step 2: Bring it into the starting position.
• Step 3: Change the counter for the unsorted array by one.
• Step 4: Repeat until all the elements are sorted.
• https://www.youtube.com/watch?v=xWBP4lzkoyM
8. • Initial list: [15, 12, 14, 16, 11, 13]
• Start with the first element (15). Assume it as the minimum.
• Scan through the remaining elements (12, 14, 16, 11, 13) and find the
smallest element, which is 11.
• Swap the minimum element (11) with the first element (15). Updated
list: [11, 12, 14, 16, 15, 13]
• Move to the next element (12) in the unsorted portion. Assume it as
the minimum.
• Scan through the remaining elements (14, 16, 15, 13) and find the
smallest element, which is 13.
• Swap the minimum element (13) with the second element (12).
Updated list: [11, 12, 14, 16, 15, 13]
• Repeat steps 4 to 6 until all elements are in their correct sorted
positions.
• Final sorted list: [11, 12, 13, 14, 15, 16]
9. Bubble sort:
• Bubble sort is a simple sorting algorithm.
• This sorting algorithm is a comparison-based algorithm
• i.e each pair of adjacent elements is compared, and the elements
are swapped if they are not in order.
• This algorithm is not suitable for large data sets.
• https://www.youtube.com/watch?v=nmhjrI-aW5o
10.
11. •begin BubbleSort(list)
• for all elements of list
• if list[i] > list[i+1]
• swap(list[i], list[i+1])
• end if
• end for
• return list
•end BubbleSort
13. Merge sort:
• Merge sort is one of the most efficient sorting algorithms.
• It is based on the divide-and-conquer strategy.
• Merge sort continuously cuts down a list into multiple sub lists until
each has only one item, then merges those sub lists into a sorted list.
17. Quick sort
• The logic is simple, we start from the leftmost element and keep track
of the index of smaller (or equal) elements as i. While traversing, if we
find a smaller element, we swap the current element with arr[i].
Otherwise, we ignore the current element.