You are on page 1of 2

Sorting

Sorting is the process of putting a list or a group of items in a specific order. Some common sorting criteria are: alphabetical or numerical. Sorting can also be done in ascending order (AZ) or descending order (Z-A). For example: Suppose we have an array X with the following elements 14, 18, 10, 16, 12, 15 Then after sorting the array in the ascending order, it will be 10, 12, 14, 15, 16, 18 There are several methods of sorting an array like bubble sort, selection sort, insertion & merge sort etc.

Bubble Sort
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping 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 gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, some other algorithms are more efficient for sorting large lists. It requires n-1 passes to sort an array with n elements. Algorithm: BUBBLE_SORT(A,N) where A is an linear array with N elements whose elements are rearranging using bubble sort using the following steps: Step 1 repeat steps 2 and 3 for I = 1 to N-1 [ I IS USED FOR PASS NUMBER] STEP 2 pass number i] STEP 3 [COMPARE AND EXCHANGE ADAJACENT ELEMENTS] IF A[J] > A[J+1] THEN TEMP = A[J] A[J] = A[J+1] A[J+1] = TEMP [END IF] [END OF STEP 2] [END OF STEP 1] STEP 4 EXIT REPEAT STEP 3 FOR J = 1 TO N-I [ iterate each element of

CITY Institute Sandeep Sirs Academy


CERTIFIED) [CONTACT 9815600235]

(ISO 9001:2008

Step-by-step example Bubble Sort

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. Three passes will be required. First Pass: ( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them. ( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. After pass 1, largest element is placed at its actual position say 8 at last position. Second Pass: (14258) (14258) ( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2 (12458) (12458) After pass 2, second largest element is placed at its actual position say 5 at second last position. Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs whole passes without any swap to know it is sorted. Third Pass: (12458) (12458) (12458) (12458) Fourth (n-1)th Pass: (12458) (12458) Now, the algorithm completes all n-1 passes and the array is get sorted.

Analysis of Bubble sort


First pass requires n-1 comparisons Second pass requires n-2 comparisons kth pass requires n-k comparisons Last pass requires only one comparisons

There for the total comparisons are: (n-1) + (n-2) + (n-3) +.+(n-k)+ + 3+2+1 = n(n-1) / 2

= (n2 n)/2 = O(n2)

CITY Institute Sandeep Sirs Academy


CERTIFIED) [CONTACT 9815600235]

(ISO 9001:2008

You might also like