Write algorithm of selection sort, bubble sort and insertion sort.

Answer: –

Selection sort: – A selection sort is a very simple and straight forward sorting technique. This sorting used by selection. After each pass, an element can be selected for sorting this process continued up to the last element and finally all elements are sorted in desired order. An algorithm of selection sort is as fallow.

Selection_sort(array,size)
Where Array: - Represents the list of elements
                Size: - Represents size of the list
Step 1:                  Current=0[initially]
Step 2:                  Repeat through step 7 while (current<size)
Step 3:                  j=current+1
Step 4:                  Repeat through step 6 while(j<size)
Step 5:                  if(array[current]>array[j]
  1. Temp=array[current]
  2. Array[current]=array[j]
  3. Array[j]=temp
Step 6:                  j=j+1
Step 7:                  Current=current+1
Step 8:                  Exit

Bubble Sort: – Bubble sort is a simple sorting technique. However, this sorting technique is not efficient in comparison to other sorting technique. But for smaller list this sorting technique work fined. Suppose we sort the element in any desired order comparison is made with two successive element from beginning to end this process continued until last element sorted element desire order an algorithm of bubble sorting technique is as shown below.

Bubble_Sort(n,list)
Where n: - Represents the number of elements in the list.
                L: - Represents the list of elements.
Step 1:                  [Initialize]
                                I=0
Step 2:                  Repeat through step 5 while (i<n)
Step 3:                  j=0
Step 4:                  Repeat through step 5 while (j<n-1)
Step 5:                  if(list[j]<list[j+1])
  1. Temp=list[j]
  2. List[j+1]=list[j]
  3. List[j]=temp
Step 6:                  Exit

Insertion sort: – Insertion sort technique used to sort element and insert it in a desire order. This technique starts from two element and continue up to the last element after each part, number of element are sorted in desire order. Finally all elements are sorted in an order. This sorting technique is just like our world life for sorting playing card or sorting currency not. For example if a bundle of currency not or playing card available in haphazard form. First of all we take two playing card and currency note and sort them fallowed by take third playing card or currency note and placed them in proper position. This process continues up to last playing card or currency note. Algorithm of insertion sort is as fallow.

Insertion_sort(l,n)
Where l: - Represents the list of elements.
                N: - Represents number of elements in the list.
Step 1:                  [Initialize]
                                L[0]=0
Step 2:                  Repeat through step 3 to 5 for i=1, 2, 3, 4 . . . n.
Step 3:
  1. Temp=l[i]
  2. Pointer=i-1
Step 4:                  while(temp<l[pointer])
  1. L[pointer+1]=l[pointer]
  2. Pointer=pointer-1
Step 5:                 L[pointer]=temp
Step 6:                  Exit
Advertisements