Bubble Sort in Data Structure
What is Bubble Sort in Data Structure-?
Bubble Sort arrange N number of array elements by placing the biggest element on proper position. It always arrange the data in descending order.
Example:- int a[5]={7, 6, 3, 4, 1} = 1, 3, 4, 6, 7
Procedure of Bubble Sort in Data Structure:-
✱ In the following given numbers 7 and 6 changes the place and rest of the numbers will be in the same place in the second line.
✱ In the second line now 7 and 3 will change the place.
✱ In the third line 7 and 4 will change the line.
✱ In the third line 4 and 7 will change the place and 7 came at the end.
✱ Remaining 6, 3, 4, 1
✱ Now the same process will be in all of the reaming numbers.
1.
7, 6, 3, 4, 1
6, 7, 3, 4, 1
6, 3, 7, 4, 1
6, 3, 4, 7, 1
6, 3, 4, 1, 7
Take (7) out and remaining 6, 3, 4, 1
2.
6, 3, 4, 1
3, 6, 4, 1
3, 4, 6, 1
3, 4, 1, 6
Take (6) out and remaining 3, 4, 1
3.
3, 4, 1
3, 4, 1 (write same because 4 is bigger then 3)
3, 1, 4 (comparison will be in 4 and 1 in the second line)
Take (4) out and remaining 3, 1
4.
3, 1
1, 3
Take (3) out and remaining 1
5.
1 (1 is the single number and it can't compare)
Now take the out numbers which is in brackets reversely
1, 3, 4, 6, 7 this is the answer.
Algorithm for Bubble Sort in Data Structure
Step 1:- Begin
Step 2:- Input a[5] 0-4
Step 3:- Set i ⬅ 3
Step 4:- Repeat step 4 to 9 while (i>=0)
Step 5:- Set j ⬅ 0
Step 6:- Repeat step 7 and 8 while (j<=i)
Step 7:- If (a[j]>a[j+1]) the
Set temp ⬅ a[j]
a[j] ⬅ a[j+1]
a[j+1] ⬅ temp
Step 8:- j ⬅ j+1
Step 9:- i ⬅ i-1
Step 10:- Print a[5]
Step 11:- Exit
What is Selection Sort in Data Structure-?
Selection Sort arrange N element of array by placing the smallest element in proper position in case of ascending order arrangement.
Example:- int a[5]={7, 6, 3, 4, 1} = 1, 3, 4, 6, 7
Procedure of Selection Sort in Data Structure:-
In this comparison will happen between smallest and biggest number in the given array number and they will change the place and make an ascending order.
1. Comparison between 7 and 1
7. 6, 3, 4, 1
1, 6, 3, 4, 7
2. Comparison between 6 and 3
1, 6, 3, 4, 7
1, 3, 6, 4, 7
3. Comparison between 6 and 4
1, 3, 6, 4, 7
1, 3, 4, 6, 7
4. Ascending order answer in 1, 3, 4, 6, 7 and for descending order it will be reverse
Algorithm for Selection Sort in Data Structure
Step 1:- Begin
Step 2:- Input a[5]
Step 3:- Set i ⬅ 0
Step 4:- Repeat step 5 to 10 while (i<4)
Step 5:- Set m ⬅ a[i], j ⬅ i+1
Step 6:- Repeat step 7 and 8 while (j<5)
Step 7:- If m>a[j] then
Set m ⬅ a[j]
Set loc ⬅ j
Step 8:- j ⬅ j+1
Step 9:- If a[loc]<a[i] then
Set temp ⬅ a[loc]
a[loc] ⬅ a[i]
a[i] ⬅ temp
Step 10:- Set i ⬅ i+1
Step 11:- Print a[5]
Step 12:- Exit
No comments:
Post a Comment