11/30/2020

sorting algorithms

 Sorting is the process given data according to order(it may be ascending or descending order) is called order. The data may be numeric or alphabetical. It is the most important operation used in computer. 

For example, a student database consist of student roll number, name, father name and address etc. Here the key field the roll number of the student. The value in this line is unique there is no same roll number for more than one students. The record of class store according to their roll numbers.

There are a  number of methods are used in sorting. The common methods that are mostly used in their algorithms are bubble sort, insertion sort and selection sort. And other which one of them is merge sort. All these algorithm are very easy and simple. Sorting into two parts ascending and descending order.  The data may be a FIFO and LIFO method. FIFO stands for first in first out and LIFO stand for last in first out.

Types of Sorting with Algorithm

 

algorithm

 

 

  • Bubble Sort
  • Insertion Sort
  • Selection Sort
  • Merge Sort

 

 

 



 


Bubble Sort

Bubble sort is the most simplest method and also slowest. It compare the first item in line with next and swap them if required. This process continue as well as long until the all items swapped. Bubble sort is mostly consider inefficient sorting. 
Suppose that, 
 

 
First of all 34 compare with 54, the 34 is smaller than 54 it sill remain.
54 is compare with 2 and both or swapped. 2 is reach in number 2 points and 54 in number 3.
In last 54 is compare with 12 and 54 is reach in last point and 12 in number 3.
Once again 34 is compare with 2 and swap both 2 is reach in first point and 34 move to the second point. This process continue as well as long until the number 2 reached in first, 12 in second, 34 in third and 54 in last point.
 
 

Algorithm

  •      SET I=S
  •      REPEAT STEP 3 TO 7 WHILE(I>=1)
  •      SET N=1
  •      REPEAT STEP 5 TO 6 WHILE(I<=N)
  •      IF A[N]>A[N+1]
  •      TEMP=A[N]
  •      A[N]=A[N+1]
  •     A[N+1]=TEMP
  •      N++
  •      I--                                  
  •      EXIT                                   
                                    

Selection Sort

In selection sort it works select the smallest unsorted with remaining in sequence and swapping all items. Selection sort is very easy to implement and very simple sort. In this list is not swapped no more than 1000 items. 
In selection statement in a list it first swapped to the next and convert if the second is smaller than first otherwise still same. after it the second is compare with three of the third is smaller than second than move it in second point and second reached in point three.
This process continue as well as long until the all items are arranged in sequence. Smallest point reached in first position and larger reached in last position.
The Algorithm of selection sort is follow:
  • set m=0
  • repeat step while(m<n)
  • temp=A[m]
  • i=m
  • Repeat step while(i<=n)
  • if temp>A[i]
  • temp=A[i]
  • NOC=i
  • end if
  • i++
  • T=A[NOC]
  • A[NOC]=A[m]
  • A[m]=t
  • m++
  • EXIT
 
 











Insertion Sort

It is twice in efficient as bubble sort but it is simple like a bubble sort. Insertion sort is simple to implement. It is also easy to implement. However, it used in large lists. 
In insertion sorting, the list is scanned from start to end. In this sort each item is insert and reached in proper place and data save in memory.
Suppose that in list 13,54,2,76 are four items. In first round no shifted in array.
Iteration two, 2 is reached in first position and 13 is reached in second, 54 is reached in third.
Iteration three, it checked 2 reached in position first, 13 reached in second position, 54 is reached in third position and 76 in last position.
Algorithm of insertion sort is follow:
  • Repeat step 2 to 6
  • T=A[S]
  • L=S
  • Repeat step 5
  • L=L-1
  • A[L]=T
  • EXIT
 
 
 
 
 
 
 
Related Topics
 



For more detail you visit
 

No comments:

Post a Comment

Thanks for Visiting