Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
My journey with you | All you wish will be here !!!
My journey with you | All you wish will be here !!!
Sorting algorithms are fundamental tools in computer science that enable efficient data organization and retrieval. In this blog post, we will explore various sorting algorithms, their significance in data structures and algorithms (DSA), and provide Java code snippets for practical implementation.
Sorting algorithms are procedures that arrange elements of a list or array in a specific order—typically ascending or descending. The ability to sort data is essential in various applications, from databases to search engines. Common sorting algorithms include:
Now, let’s look at some commonly used sorting algorithms, along with their Java implementations.
Bubble Sort is a simple algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. Its average and worst-case time complexity is O(n²).
javaCopy codepublic class BubbleSort {
public static 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] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
Selection Sort divides the input list into a sorted and an unsorted region. It repeatedly selects the smallest (or largest) element from the unsorted region and moves it to the sorted region.
javaCopy codepublic class SelectionSort {
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
Insertion Sort builds the final sorted array one item at a time. It is efficient for small datasets, with a time complexity of O(n²) in the worst case but O(n) in the best case.
javaCopy codepublic class InsertionSort {
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements greater than key to one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
}
Merge Sort is a divide-and-conquer algorithm that divides the list into halves, recursively sorts them, and then merges the sorted halves. It has a time complexity of O(n log n).
javaCopy codepublic class MergeSort {
public static void mergeSort(int[] arr) {
if (arr.length < 2) return;
int mid = arr.length / 2;
int[] left = new int[mid];
int[] right = new int[arr.length - mid];
for (int i = 0; i < mid; i++) left[i] = arr[i];
for (int i = mid; i < arr.length; i++) right[i - mid] = arr[i];
mergeSort(left);
mergeSort(right);
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) arr[k++] = left[i++];
else arr[k++] = right[j++];
}
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
}
}
Quick Sort is another divide-and-conquer algorithm that selects a ‘pivot’ element and partitions the other elements into two sub-arrays, recursively sorting them. Its average time complexity is O(n log n).
javaCopy codepublic class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap arr[i + 1] and arr[high] (or pivot)
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
Heap Sort uses a binary heap data structure to sort elements. It has a time complexity of O(n log n).
javaCopy codepublic class HeapSort {
public static void heapSort(int[] arr) {
int n = arr.length;
// Build heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// One by one extract elements from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// Call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
private static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) largest = left;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest != i) {
// Swap
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
}
Understanding sorting algorithms is crucial for anyone looking to excel in data structures and algorithms. These algorithms form the foundation for more complex operations and are widely applicable in real-world scenarios. By mastering these algorithms, you will equip yourself with essential tools for effective problem-solving in programming.
Stay tuned for our next post, where we’ll dive deeper into specific sorting algorithms and their performance analysis!
Also see: The Z Blogs
my other Blog: The Z Blog ZB
Hi! I could have sworn I’ve been to this blog before but after reading through some of the post
I realized it’s new to me. Nonetheless, I’m definitely
happy I found it and I’ll be bookmarking and checkinng
bacck frequently! https://evolution.org.ua/
Hi! I could have sworn I’ve been to this blog before but
after reading through some of thee post I realized it’s new to me.
Nonetheless, I’m definitely happy I found it andd I’ll
be bookmjarking and checking back frequently! https://evolution.org.ua/