Bubble Sort
Bubble Sort
Group: 06
Nimra Shabbir (5002)
Zainab Naafey (1053)
Syeda Kiran (1055)
Topic:
Bubble sort
Submitted to:
Prof. Sobia Shahid
Sorting by exchange:
We will discuss set of sorting techniques based on the
principle of sorting by exchange.
“The exchange sort is a sorting algorithm that
compares each item with other items of the array and
swap the item if requires.”
Following are the techniques based on this principle:
Bubble sort.
Shell sort.
Quick sort.
Bubble sort:
“Bubble sort is a simple algorithm that compares the
first element of the array to the next one.”
It is the simplest sorting techniques among all the
sorting techniques known. This technique is simple in the
sense that is easy to understand, easy to implement and
easy to analyze. It is a comparison-based algorithm. If
the current element of the array is numerically greater
than the next one, the elements are swapped. Likewise,
the algorithm will traverse the entire element of the
array.
Working:
Let us consider that n elements to be sorted are stored in
an array.
1)While location of the first element is not the same as
the location of the last elements. (do steps 2-7)
2)Let’s start with the first element as the current
element.
3)While current element is not the same as the last
element, (repeat step 4-6).
4)Compare the current element with the immediate
next element.
5)If the two elements are not in the order, then swap
the two.
6)Move to the next element, and it is now the current
element.
7)The largest element is now placed at its right position.
The element just ahead to this element becomes the
last element of the rest of the unsorted list. (Go to
step 1)
8)Exit.
Explanation:
From the above description of the
bubble sort, we that it consists of two loops: outer loop
(step 1-7), and inner loop (step 3-6). To sort n number of
elements, outer loop is executed (n-1) times. The first
loop rolls the inner loop (n-1) times, the second loop rolls
the inner loop (n-2) times and so on.
The bubble sort derives its name from the fact that the
smaller data items “bubble up” in the top of the list. This
method is also alternatively term “sinking sort” because
the larger elements sink down to the bottom of the list.
Algorithm:
1) For i=0 to n-1 do //outer loop with n-1 passes
2) For j=0 to n-i-1 do //inner loop : in the ith pass
3) If(A[j]>A[j+1])then // comparing two elements to check the order
4) Swap(A[j], A[j+1]) //interchange A[j] and A[j+1]
5) End if
6) End for
7) End for
8) Exit.
Example:
For example, we take an unsorted array. Bubble sort
takes Ο(n2) (represent the complexity of an algorithm whose performance directly proportional to square size of the input
data) time so we're keeping it short and precise.
4. Fourth step:
New array will look like this after swapping.
5. Fifth step:
Now we will compare 33 and 35. These values are
already in sorted position.
6. Sixth step:
We will compare 35 and 10. So, 33 and 10 needs to be
swapped because element 33 is greater than 10.
7. Seventh step:
New array will look likes this
8. Fourth step:
These 8 steps above were just 1st loop pass.
Now we will show what array will looks like after every
loop pass.
After 1st loop pass, largest value will be sorted in the last.
}
cout << endl;
int main()
int array[i];
int k;
char a[k];
//Numeric array
cout << "Enter the size of Your array :" << endl;
cin >> size;
cout << "Enter the size for alphabetical array :" << endl;
cin >> k;
cout << "Enter the elements of alphabetical array :" << endl;
display_array(array, size);
bubble_sort(array, size);
display_array(array, size);
print_alpha(a, k);
sort_alpha(a, k);
print_alpha(a, k);
return 0;
getch();
}
Advantages:
Easy to implement and understand.
Stable sorting algorithm.
Useful in computer graphics to resolve very small
errors.
Its space complexity is very less in comparison to
other sorting algorithms.
Disadvantages:
It is the high time complexity of O(n2) and therefore
higher execution time.
It is not preferable for a large amount of data.
Conclusion:
The bubble sort algorithm is the easiest out of all sorting
algorithms in terms of implementation. However, it takes
a lot of time for execution when the amount of data is
large. Thus, when the data set is small, bubble sort is one
of the best algorithms to apply.