Singly Selection List
Singly Selection List
GROUP NUMBER 4
SEMESTER : TWO
GROUP FIVE
are a set of techniques used to rearrange elements of a list or an array in a specific order. The
most common orderings are numerical or lexicographical (alphabetical). Sorting is a fundamental
operation in computer science and is used in various applications, including searching, data
compression, and data analysis
There are numerous sorting algorithms, each with its own advantages and disadvantages in terms
of performance, stability, and ease of implementation. Some of the most well-known sorting
algorithms include:
a) Insertion Sort:
Insertion sort builds the final sorted list one item at a time by repeatedly taking the next element
from the unsorted part and inserting it into its correct position in the sorted part
Advantages of the insertion sort
Insertion sort has a lot of swaps which can make it slow on Morden computers
Inefficient for large data sets or reverse-ordered data with a worst-case time complexity
int n = arr.length;
for (int i = 1; i < n; ++i) {
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int n = arr.length;
System.out.println();
System.out.println("Original array:");
printArray(arr);
insertionSort(arr);
System.out.println("Sorted array:");
printArray(arr);
b) Bubble sort
is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. The pass through the list is
repeated until the list is sorted. It's called "Bubble Sort" because smaller elements
gradually "bubble" to the top of the list.
Due to that the folowings are the advantages and the dis-advantages of the bubble sort
c) Merge Sort
is a divide-and-conquer algorithm used for sorting arrays (or lists).
the merge sort it works as follows:
•
Divide:
The array is divided into two (or more) subarrays recursively until each subarray
contains only one element. This process continues until the base case is reached, i.e., the
subarrays contain only one element each.
Conquer:
The divided subarrays are then merged back together in a sorted manner. This merging
process occurs recursively, combining two smaller sorted arrays (subarrays) into a larger
sorted array until the entire array is sorted.
Merge:
During the merge step, the two subarrays are merged together into a single sorted array.
This is done by comparing the elements of the two subarrays one by one and selecting the
smaller (or larger) element first. The selected element is then placed into a new array.
This process continues until all elements from both subarrays are merged into the new
array.
Advantages of the merge sort
Guaranteed performance:
Merge Sort has a time complexity of O(n log n) in all cases, making it more efficient than
sorting algorithms like Bubble Sort or Insertion Sort, especially for large datasets.
Stable sorting:
Merge Sort is stable, meaning it preserves the relative order of equal elements in the
sorted array.
Advantages continue………
Parallelizable:
Merge Sort can take advantage of parallelism, as the merging of sorted subarrays can be
done independently.
Example of java code in merge sort
public class MergeSort {
public static void mergeSort(int[] arr) {
if (arr == null || arr.length < 2) return;
sort(arr, 0, arr.length - 1); }
private static void sort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
// Sort first and second halves
sort(arr, left, mid);
sort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right); } }
private static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
// Create temporary arrays
int[] L = new int[n1];
int[] R = new int[n2];
// Copy data to temporary arrays
for (int i = 0; i < n1; ++i)
L[i] = arr[left + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[mid + 1 + j];
// Merge the temporary arrays
int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++; } else {
arr[k] = R[j];
j++; }
k++;
}
// Copy remaining elements of L[] if any
while (i < n1) {
arr[k] = L[i];
i++;
k++; }
// Copy remaining elements of R[] if any
while (j < n2) {
arr[k] = R[j];
j++;
k++; } }
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
System.out.println("Original array:");
printArray(arr);
mergeSort(arr);
System.out.println("Sorted array:");
printArray(arr); }
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); }
System.out.println(); }}