Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Презентация:



Advertisements
Похожие презентации
Influence of video’s sound quality on its positions in YouTube search results - SeeZisLab
Advertisements

Goal: to use inequalities involving angles and sides of triangles Activities: 1.Open GSP 4.06 and complete all steps and answer all questions for GSP Triangle.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
AVL-Trees COMP171 Fall AVL Trees / Slide 2 Balanced binary tree * The disadvantage of a binary search tree is that its height can be as large as.
DRAFTING TECHNIQUES I 136. Here is a basic shape. From here, we will do some advanced drafting once we put this shape on a sheet as a drawing. Select.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
Indirect Questions How do you make indirect questions? When do you use this grammar?
The idea of creating a device to control operation of the computer, which we now call a mouse, belongs to the American scientist Doug Engelbart (born.
There is no place like home. four – the fourth one – the first ten – the tenth five – the fifth twelve – the twelfth two – the second There is a number.
A Bill is a proposal for a new law, or a proposal to change an existing law that is presented for debate before Parliament. Bills are introduced in either.
What to expect? How to prepare? What to do? How to win and find a good job? BUSINESS ENGLISH COURSE NOVA KAKHOVKA GUMNASUIM 2012.
The problem of String Matching Given a string S, the problem of string matching deals with finding whether a pattern p occurs in S and if p does occur.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
Travel 03-04/12/2015. English Travel Vocabulary: Planning a trip vocabulary-planning-a-trip/
By Savruyeva Alima. Study schedule strategy That Actually Works!
THE TERM POLITICAL LEADERSHIP Adopted:Batyrbekkyzy Gaukhar Prepared: Kayupova.E Group: юм 14-3 р.
Influence of property optimization of the uploaded file on its positions in Youtube search results - SeeZisLab
THE HLB SYSTEM Done by: Abdykadyrkyzy A., Nurkair A.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
TASK1 Guess and spell the words.. 1 OTBLET 2 RUTCAINS 3 VIKSEN 4 ZAEGMINAS 5 HOSEWR 6 YAJPAMS 7 SIBCUTIS 8 KEBNALT 9 IRCAH 1-bottle 2-curtains 3-knives.
Транксрипт:

Bubble Sort

Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12 The 12 is greater than the 7 so they are exchanged. The 12 is greater than the 3 so they are exchanged. The twelve is greater than the 9 so they are exchanged The 12 is larger than the 11 so they are exchanged. In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list. Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged. Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged. The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

Bubble Sort Example 6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons. First Pass Second Pass

Bubble Sort Example 2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

Bubble Sort Example 2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass Each pass requires fewer comparisons. This time only 4 are needed. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass

Bubble Sort Example 2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass

Bubble Sort Example 2, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass Sixth Pass 2, 3, 6, 7, 9, 9, 11, 12 This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

Bubble Sort Example Questions 1.Which number is definitely in its correct position at the end of the first pass? 2.How does the number of comparisons required change as the pass number increases? 3.How does the algorithm know when the list is sorted? 4.What is the maximum number of comparisons required for a list of 10 numbers?

Answers 1.The last number must be the largest. 2.Each pass requires one fewer comparison than the last. 3.When a pass with no exchanges occurs. 4.9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45

Pointer to Functions A simpler way: – You may define 6 comparison functions to do this: int bsort(int a[], int n, int (*pfun)(int a, int b) ) { int i, j, temp; for (i=n-1; i>=1; i--) for (j=0; j<i; j++) if (pfun(a[j], a[j+1]) > 0 ) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } return 0; }