Data structure - Sorting methodes

Course-Data Structures | Talenthome 

Sorting methods

1)    Bubble sort –

Sorting is a technique to rearrange in data structure. Here in bubble sort adjacent element if first element is greater than next element then swapping occurs. In the same way between inner loop and outer loop goes continuously and all the elements gets compared and swapped for all the arrangement.

 

Algorithm for bubble sort

 

1.       Repeat for i=1 to n.

2.       Repeat for j=1 to n.

3.       If LA[j]>LA[j+1],

Then:swap.

[end of if]

[end of step 2 loop]

[end of step 1 loop]

4.       Exit .  

 

2)    Selection sort –

As per its name in this sorting method the elements are compared with all other position that’s why the smallest element will be automatically comes to 1st position. In the same way loops goes continuously from first place to last place, finally swapping takes place.

 

Algorithm for selection sort

 

1.       Repeat for i=1 to n.

2.       Repeat for j=i+1 to n.

3.       If LA[i]>LA[j],

Then:swap.

[end of if]

[end of step 2 loop]

[end of step 1 loop]

4.       Exit .

 

3)    Insertion sort –

This is third type of sorting technique which is used to quickly sort data structure elements. It is one of the simplest shortest techniques. This sorting technique works quite differently as compared to bubble and selection sort.

Loop starts from second position and the second position element will be compared with first position element. If it founds smaller interchange will takes place. In the same way next element will be compared with the previous element will be swapping takes place if required and loops will go on continuously till complete data structure does not get sorted.

 

  Algorithm for insertion sort

 

 

1.       Repeat for i=2 to n.

2.       Repeat for j=2 to n.

3.       If LA[i]>LA[j-1],

Then:swap.

[end of if]

[end of step 2 loop]

[end of step 1 loop]

4.       Exit .

 

 

 

Comments

Popular posts from this blog

Theory part introduction