cs notebook1
  • Introduction
  • sort algorithm
  • Overloading VS Overriding
  • multithreading
  • Concurrency0
  • Concurrency1
  • ExecutorService
  • iteration & recursion
  • IO
  • Marker interface(Serializable, Clonnable&Remote)
  • Jackson Library
  • java.lang.System.*
  • Virtual Memory
  • Java ClassLoader
  • interfaceVSabstractClass
  • ENUM
Powered by GitBook
On this page
  • 1. Bubble Sort
  • 2. Merge Sort
  • 3. QuickSort
  • 4. Bucket Sort
  • 5. Heap Sort
  • 6. Cocktail Sort
  • 7. Insertion Sort

Was this helpful?

sort algorithm

1. Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Example: First Pass: (514 2 8 ) –> (154 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1542 8 ) –> ( 1452 8 ), Swap since 5 > 4 ( 1 4528 ) –> ( 1 4258 ), Swap since 5 > 2 ( 1 4 258) –> ( 1 4 258), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Second Pass: (142 5 8 ) –> (142 5 8 ) ( 1425 8 ) –> ( 1245 8 ), Swap since 4 > 2 ( 1 2458 ) –> ( 1 2458 ) ( 1 2 458) –> ( 1 2 458) Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs onewholepass withoutanyswap to know it is sorted.

Third Pass: (124 5 8 ) –> (124 5 8 ) ( 1245 8 ) –> ( 1245 8 ) ( 1 2458 ) –> ( 1 2458 ) ( 1 2 458) –> ( 1 2 458)

    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 temp and arr[i]
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
    }

2. Merge Sort

/* Java program for Merge Sort */
class MergeSort
{
    // Merges two subarrays of arr[].
    // First subarray is arr[l..m]
    // Second subarray is arr[m+1..r]
    void merge(int arr[], int l, int m, int r)
    {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        int L[] = new int [n1];
        int R[] = new int [n2];

        /*Copy data to temp arrays*/
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];


        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        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++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
    void sort(int arr[], int l, int r)
    {
        if (l < r)
        {
            // Find the middle point
            int m = (l+r)/2;

            // Sort first and second halves
            sort(arr, l, m);
            sort(arr , m+1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }

3. QuickSort

4. Bucket Sort

The idea is to use bucket sort. Following is bucket algorithm.

bucketSort(arr[], n)

1) Create n empty buckets (Or lists).
2) Do following for every array element arr[i].
.......a) Insert arr[i] into bucket[n*array[i]]
3) Sort individual buckets using insertion sort.
4) Concatenate all sorted buckets.

5. Heap Sort

6. Cocktail Sort

two way bubble sort

  1. The first stage loops through the array from left to right, just like the Bubble Sort. During the loop, adjacent items are compared and if value on the left is greater than the value on the right, then values are swapped. At the end of first iteration, largest number will reside at the end of the array.

  2. The second stage loops through the array in opposite direction- starting from the item just before the most recently sorted item, and moving back to the start of the array. Here also, adjacent items are compared and are swapped if required.

7. Insertion Sort

Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.

Algorithm

// Sort an arr[] of size n

insertionSort(arr, n)

Loop from i = 1 to n-1.

……a) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]

Uses:

Insertion sort is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.

PreviousIntroductionNextOverloading VS Overriding

Last updated 5 years ago

Was this helpful?

Like , Merge Sort is a algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.

Cocktail Sort is a variation of. The Bubble sort algorithm always traverses elements from left and moves the largest element to its correct position in first iteration and second largest in second iteration and so on. Cocktail Sort traverses through a given array in both directions alternatively. Algorithm: Each iteration of the algorithm is broken up into 2 stages:

QuickSort
Divide and Conquer
Bubble sort