CPP Notes
Notes
Que & Ans
For ex.:- It is relatively easy to look up the phone number of a friend from a telephone directory because the names of the phone book have been sorted into alphabetical order.
The example clearly illustrates one of the main reason that sorting large quantities of information is desirable. That is, sorting greatly improves the efficiency searching. If we were to open a phonebook, and find the names were not presented in any logical order, it would take an incredibly long time to lookup some ones phone number.
Sorting can be performed using several methods, they are :-
1. Insertion Sort
2. Merge Sort
3. Quick Sort
4. Heap Sort
5. Selection Sort
6. Bubble Sort
When
the first pass through the array is complete the array is complete the
bubble sort returns to elements one and two and stars the process all
over again.
So when does it stop ?
Output:
Explain Bubble Sorting With Example
15 June
2
What is Bubble Sorting ? Explain Bubble Sorting in Array with example(C++) ?
Sorting Refers to ordering data in an increasing & decreasing fashion according to some linear relationship among the data items. Sorting can be done on name, numbers, records etc,For ex.:- It is relatively easy to look up the phone number of a friend from a telephone directory because the names of the phone book have been sorted into alphabetical order.
The example clearly illustrates one of the main reason that sorting large quantities of information is desirable. That is, sorting greatly improves the efficiency searching. If we were to open a phonebook, and find the names were not presented in any logical order, it would take an incredibly long time to lookup some ones phone number.
Sorting can be performed using several methods, they are :-
1. Insertion Sort
2. Merge Sort
3. Quick Sort
4. Heap Sort
5. Selection Sort
6. Bubble Sort
Bubble Sorting :-
This technique is named so because of the logic is similar to bubble in
water. When a bubble is formed it is small at the bottom and when it
moves up it becomes bigger and bigger. i.e. bubbles are In ascending
order of their size from the bottom to the top.
The
bubble sort repeatedly compare adjacent element of an array. The first
and second elements are compared and swapped if out of order. Then the
second and third elements are swapped if out of order. The sorting
process sorting process continues until the last two elements of the
array are compared and swapped if out of order.
For ex.:- 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 necessary swapping adjacent elements.
Several passes have to be made through the array before it is finally
sorted.
Phase 1 :-
|
|
Array at beginning
|
67,23,79,82,69
|
Pass 1
|
23,67,79,82,69
|
Pass 2
|
23,67,79,82,69
|
Pass 3
|
23,67,79,69,82
|
Phase 2 :-
|
|
Pass 1
|
23,67,79,69,82
|
Pass 2
|
23,67,79,69,82
|
Pass 3
|
23,67,79,69,82
|
So when does it stop ?
It is finished when it examines the entire array and no “swaps” are needed (thus the list is in proper order).
The bubble sort is an easy algorithm to program but it is slower then
many other sorts with a bubble sort, it is always necessary to make one
final “Pass” through the array to check to see that no swaps are made
to ensure that the process is finished. In actuality, the process is
finished before thus last part is made.
A SIMPLE PROGRAM OF BUBBLE SORTING TO SORT ARRAY I N ASCENDING ORDER :-
Program :
Program :
Output:
Must Read :-
Previous article
Next article
Really good answer in simple words...
ReplyDeleteNew people also understand
Thank You for your feedback .. :)
Delete