Bubble sorting lab manual

5
Name Reg # Grade BUBBLE SORTING Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position. Aim: Write a program to Sort values in Ascending or Descending Order using Bubble Sort Technique in Linear Array. Theory: The first element in the list is compared with the second. If the first element is found greater than the second, then both are made to swap places. Now the second element is compared with the third and again if second is greater than the third, they are swapped, . This process is repeated for all the elements. Finally the greatest value in the list reaches the last index. Now one number is at its correct place so to sort the whole array, the complete above process is repeated equal to the number of elements in the list. //Suppose the array arr[] has to be sorted in ascending oder

Transcript of Bubble sorting lab manual

Page 1: Bubble sorting lab manual

NameReg #Grade

BUBBLE SORTINGBubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position.

Aim: Write a program to Sort values in Ascending or Descending Order using Bubble Sort Technique in Linear Array.

Theory:

The first element in the list is compared with the second. If the first element is found greater than the second, then both are made to swap places. Now the second element is compared with the third and again if second is greater than the third, they are swapped, . This process is repeated for all the elements. Finally the greatest value in the list reaches the last index. Now one number is at its correct place so to sort the whole array, the complete above process is repeated equal to the number of elements in the list.

//Suppose the array arr[] has to be sorted in ascending oder//and  there are N elements:

Begin

For p = 0 To N-1 Loop

For q = 0 To Less Than N_1 LOOP

If arr[q]>arr[q+1] Then

Interchange arr[q] with arr[q+1]

EndifEndfor ,EndforStop

Algorithm:

Page 2: Bubble sorting lab manual

(Bubble Sort) BUBBLE (A, N)Here A is an Array with N elements stored in it. This algorithm sortsthe elements in A.1. Repeat Steps 2 to 4 for Pass = 1 to N – 12. Set Swapped = 0 and K = 03. Repeat while K < (N – Pass)(a) if A[ K ] > A[ K + 1] thenInterchange A[ K ] and A[ K + 1 ] andSet Swapped = 1[ End of if structure. ](b) Set K = K + 1[ End of inner loop of step-3. ]4. if Swapped = 0 then break the outer loop of step-1[ End of outer loop of step-1. ]5. End Example:

When this first pass through the array is complete, the bubble sort returns to elements one and two and starts the process all over again. So, when does it stop? The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order). The bubble sort keeps track of occurring swaps by the use of a flag.

The table below follows an array of numbers before, during, and after a bubble sort for descending order. A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements. Several passes have to be made through the array before it is finally sorted.

Array at beginning: 84 69 76 86 94 91

After Pass #1: 84 76 86 94 91 69

After Pass #2: 84 86 94 91 76 69

After Pass #3: 86 94 91 84 76 69

After Pass #4: 94 91 86 84 76 69

After Pass #5 (done): 94 91 86 84 76 69

Code:

Page 3: Bubble sorting lab manual
Page 4: Bubble sorting lab manual

Result:

***********