Sort
Sort
Sorting and searching are among the most common programming processes. We want to keep information in a sensible order. alphabetical order ascending/descending order order according to names, ids, years, departments etc.
Selection Sort
The list is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. We find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. A list of n elements requires n-1 passes to completely rearrange the data.
The aim of sorting algorithms is to put unordered information in an ordered form. There are many sorting algorithms, such as: Selection Sort Bubble Sort Insertion Sort Merge Sort Quick Sort The first three are the foundations for faster and more efficient algorithms.
23 8 8 8 8 8
78 78 23 23 23 23
45 45 45 32 32 32
8 23 78 78 45 45
32 32 32 45 78 56
56 56 56 56 56 78
Original List
After pass 5
Bubble Sort
The list is divided into two sublists: sorted and unsorted. The smallest element is bubbled from the unsorted list and moved to the sorted sublist. After that, the wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. Each time an element moves from the unsorted part to the sorted part one sort pass is completed. Given a list of n elements, bubble sort requires up to n -1 passes to sort the data. Bubble sort was originally written to bubble up the highest element in the list. From an efficiency point of view it makes no difference whether the high element is bubbled or the low element is bubbled.
23 8 8 8 8
78 23 23 23 23
45 78 32 32 32
8 45 78 45 45
32 32 45 78 56
56 56 56 56 78
Original List
Insertion Sort
Most common sorting technique used by card players. Again, the list is divided into two parts: sorted and unsorted. In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. A list of n elements will take at most n-1 passes to sort the data.
23 78 45 8 23 78 45 8 23 45 78 8 8 8 8 23 45 78 23 32 45 23 32 45
32 56 32 56 32 56 32 56 78 56 56 78
Original List After pass 1 After pass 2 After pass 3 After pass 4
After pass 5
10
11