Computer Science, asked by Raiyankhan2845, 1 year ago

Example for all sorting algorithms and its procedure

Answers

Answered by gurukulamdivya
2

Answer:

Bucket Sort

Suppose we need to sort an array of positive integers {3,11,2,9,1,5}. A bucket sort works as follows: create an array of size 11. Then, go through the input array and place integer 3 into a second array at index 3, integer 11 at index 11 and so on. We will end up with a sorted list in the second array.

Suppose we are sorting a large number of local phone numbers, for example, all residential phone numbers in the 412 area code region (about 1 million) We sort the numbers without use of comparisons in the following way. Create an a bit array of size 107. It takes about 1Mb. Set all bits to 0. For each phone number turn-on the bit indexed by that phone number. Finally, walk through the array and for each bit 1 record its index, which is a phone number.

We immediately see two drawbacks to this sorting algorithm. Firstly, we must know how to handle duplicates. Secondly, we must know the maximum value in the unsorted array.. Thirdly, we must have enough memory - it may be impossible to declare an array large enough on some systems

Bubble Sort

The algorithm works by comparing each item in the list with the item next to it, and swapping them if required. In other words, the largest element has bubbled to the top of the array. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items.

void bubbleSort(int ar[])

{

  for (int i = (ar.length - 1); i >= 0; i--)

  {

     for (int j = 1; j ≤ i; j++)

     {

        if (ar[j-1] > ar[j])

        {

             int temp = ar[j-1];

             ar[j-1] = ar[j];

             ar[j] = temp;

  } } } }

The algorithm works by selecting the smallest unsorted item and then swapping it with the item in the next position to be filled.

The selection sort works as follows: you look through the entire array for the smallest element, once you find it you swap it (the smallest element) with the first element of the array. Then you look for the smallest element in the remaining array (an array without the first element) and swap it with the second element. Then you look for the smallest element in the remaining array (an array without first and second elements) and swap it with the third element, and so on. Here is an example,

void selectionSort(int[] ar){

  for (int i = 0; i ‹ ar.length-1; i++)

  {

     int min = i;

     for (int j = i+1; j ‹ ar.length; j++)

           if (ar[j] ‹ ar[min]) min = j;

     int temp = ar[i];

     ar[i] = ar[min];

     ar[min] = temp;

} }

To sort unordered list of elements, we remove its entries one at a time and then insert each of them into a sorted part (initially empty):

void insertionSort(int[] ar)

{

  for (int i=1; i ‹ ar.length; i++)

  {

     int index = ar[i]; int j = i;

     while (j > 0 && ar[j-1] > index)

     {

          ar[j] = ar[j-1];

          j--;

     }

     ar[j] = index;

} }

Mergesort

Merge-sort is based on the divide-and-conquer paradigm. It involves the following three steps:

Divide the array into two (or more) subarrays

Sort each subarray (Conquer)

Merge them into one (in a smart way!)

Similar questions