Sorting Algorithms
Sorting Algorithms
Sorting Algorithms
Bubble Sort utilizes 2 nested for loops to sort. During the nth pass of the first for loop,
the second for loop iterates through the indexes where the adjacent elements are
swapped if necessary. Thus, the reason why it is called bubble sort is because the way
the elements are swapped. This code is the best code to write for beginners as it is very
simple to write. The Logic can easily be thought of as the programmer writes the
iteration cycles on a sheet of paper.
Example code:
Official code in Geeks For Geek:
class BubbleSort {
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
Selection sort uses 2 nested for loops too. However, Instead of behaving like bubble sort, it
actually compares the number that the 1st for loop points to the numbers above that index.
Thus, Selection sort is very versatile for applications where lots of work is needed. It is only a
pinch complex than bubble sort and its time complexity is always 0(N^2).
Example code:
Official code in Geeks For Geek:
// Java program for implementation of Selection Sort
import java.io.*;
public class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
Similar to how you insert files between a date when you are sorting time stamped files, insertion
sort works the same. Even though it may seem like bubble sort for how it swaps the indexes of
an array, it is not. It is on the complex side but it can be versatile. In addition, you can either use
a nested for loop or a for and while loop.
Example code:
Quick sort is quick and versatile especially when lots of work is required. It does rely on a pivot
value to partition the array which can be complicated for some people. Thus, you never begin
with quick sort as your first sorting algorithm. It may even require recursion to work properly.
Example code:
In GFG:
// Java implementation of QuickSort
import java.io.*;
https://www.geeksforgeeks.org/quick-sort/
class GFG {
// pivot
int pivot = arr[high];
// Increment index of
// smaller element
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 10, 7, 8, 9, 1, 5 };
int n = arr.length;
quickSort(arr, 0, n - 1);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}
Merge Sort
Merge sort is the most complex code. It splits arrays into subarrays which then gets merged
together. In the merge function (or any lines of code that you will use to merge arrays), it
assigns value from subarrays to the main array in an efficient way. As the subarrays itself is
sorted, you can merge by comparing the values of subarrays from the left first to decide how to
insert the elements. When one subarray is completed, you can simply insert elements from the
next element to the rightmost element (--> direction).
https://textbooks.cs.ksu.edu/cc310/7-searching-and-sorting/16-merge-sort-pseudocode/
https://drive.google.com/file/d/1IrJkRFBEw4GU80ukWO4P0Sh7gC-Zu7wn/view?usp=share_link