ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

Σχετικά έγγραφα
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

2 Composition. Invertible Mappings

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Section 8.3 Trigonometric Equations

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

9.09. # 1. Area inside the oval limaçon r = cos θ. To graph, start with θ = 0 so r = 6. Compute dr

EE512: Error Control Coding

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

Capacitors - Capacitance, Charge and Potential Difference

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

Συστήματα Διαχείρισης Βάσεων Δεδομένων

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Section 7.6 Double and Half Angle Formulas

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

The Simply Typed Lambda Calculus

C.S. 430 Assignment 6, Sample Solutions

derivation of the Laplacian from rectangular to spherical coordinates

[1] P Q. Fig. 3.1

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

Math221: HW# 1 solutions

Finite Field Problems: Solutions

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

Srednicki Chapter 55

Προαπαιτούμενες Ασκήσεις 5 ου Εργαστηρίου. Dose stoixeio (integer) : 25 Found stoixeio in position 7 Dose stoixeio (integer) :94 Value not found

Homework 3 Solutions

Σημειώσεις δεύτερης εβδομάδας

the total number of electrons passing through the lamp.

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

Areas and Lengths in Polar Coordinates

The challenges of non-stable predicates

department listing department name αχχουντσ ϕανε βαλικτ δδσϕηασδδη σδηφγ ασκϕηλκ τεχηνιχαλ αλαν ϕουν διξ τεχηνιχαλ ϕοην µαριανι

Right Rear Door. Let's now finish the door hinge saga with the right rear door

Areas and Lengths in Polar Coordinates

Example Sheet 3 Solutions

Approximation of distance between locations on earth given by latitude and longitude

Concrete Mathematics Exercises from 30 September 2016

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

ST5224: Advanced Statistical Theory II

Απόκριση σε Μοναδιαία Ωστική Δύναμη (Unit Impulse) Απόκριση σε Δυνάμεις Αυθαίρετα Μεταβαλλόμενες με το Χρόνο. Απόστολος Σ.

Instruction Execution Times

Math 6 SL Probability Distributions Practice Test Mark Scheme

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

4.6 Autoregressive Moving Average Model ARMA(1,1)

Μηχανική Μάθηση Hypothesis Testing

Example of the Baum-Welch Algorithm

Every set of first-order formulas is equivalent to an independent set

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

Solutions to Exercise Sheet 5

Pg The perimeter is P = 3x The area of a triangle is. where b is the base, h is the height. In our case b = x, then the area is

Code Breaker. TEACHER s NOTES

Section 9.2 Polar Equations and Graphs

Bounding Nonsplitting Enumeration Degrees

Lecture 2. Soundness and completeness of propositional logic

Tridiagonal matrices. Gérard MEURANT. October, 2008

Partial Trace and Partial Transpose

Main source: "Discrete-time systems and computer control" by Α. ΣΚΟΔΡΑΣ ΨΗΦΙΑΚΟΣ ΕΛΕΓΧΟΣ ΔΙΑΛΕΞΗ 4 ΔΙΑΦΑΝΕΙΑ 1

CHAPTER 48 APPLICATIONS OF MATRICES AND DETERMINANTS

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

TMA4115 Matematikk 3

Second Order RLC Filters

5.4 The Poisson Distribution.

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 10η: Basics of Game Theory part 2 Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

Στο εστιατόριο «ToDokimasesPrinToBgaleisStonKosmo?» έξω από τους δακτυλίους του Κρόνου, οι παραγγελίες γίνονται ηλεκτρονικά.

Fractional Colorings and Zykov Products of graphs

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

forms This gives Remark 1. How to remember the above formulas: Substituting these into the equation we obtain with

Assalamu `alaikum wr. wb.

Modbus basic setup notes for IO-Link AL1xxx Master Block

ΕΙΣΑΓΩΓΗ ΣΤΗ ΣΤΑΤΙΣΤΙΚΗ ΑΝΑΛΥΣΗ

LESSON 14 (ΜΑΘΗΜΑ ΔΕΚΑΤΕΣΣΕΡΑ) REF : 202/057/34-ADV. 18 February 2014

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

Durbin-Levinson recursive method

On a four-dimensional hyperbolic manifold with finite volume

6.3 Forecasting ARMA processes

Matrices and vectors. Matrix and vector. a 11 a 12 a 1n a 21 a 22 a 2n A = b 1 b 2. b m. R m n, b = = ( a ij. a m1 a m2 a mn. def

Δημιουργία Λογαριασμού Διαχείρισης Business Telephony Create a Management Account for Business Telephony

ΑΡΙΣΤΟΤΕΛΕΙΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΘΕΣΣΑΛΟΝΙΚΗΣ ΤΜΗΜΑ ΟΔΟΝΤΙΑΤΡΙΚΗΣ ΕΡΓΑΣΤΗΡΙΟ ΟΔΟΝΤΙΚΗΣ ΚΑΙ ΑΝΩΤΕΡΑΣ ΠΡΟΣΘΕΤΙΚΗΣ

FORTRAN & Αντικειμενοστραφής Προγραμματισμός ΣΝΜΜ 2017

Other Test Constructions: Likelihood Ratio & Bayes Tests

D Alembert s Solution to the Wave Equation

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

ΗΥ-150. Ταξινόµηση και Αναζήτηση

Problem Set 3: Solutions

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

(1) Describe the process by which mercury atoms become excited in a fluorescent tube (3)

Advanced Subsidiary Unit 1: Understanding and Written Response

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 7η: Consumer Behavior Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

Inverse trigonometric functions & General Solution of Trigonometric Equations

Εργαστήριο 7: Ο αλγόριθμος ταξινόμησης Radix Sort

Transcript:

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ Κωδικός Θ: ΤΠ3001, Κωδικός Ε: ΤΠ3101 (ΜΕΥ/Υ) Ώρες (Θ - Ε): 4-2 Προαπαιτούμενα: Δρ. ΒΙΔΑΚΗΣ ΝΙΚΟΣ

ΕΡΓΑΣΤΗΡΙΟ 4 Ταξινόμηση με 1. Bubble Sort 2. Quick Sort

Bubble Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 3

Bubble Sort Introduction Bubble sort is also known as exchange sort. Bubble sort is the simplest sorting algorithm. In bubble sort algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follow the same steps repeatedly until the values of array is sorted. In practice it is used rarely and its main application is to make an introduction to the sorting algorithms In worst-case the complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is Ω(n). Quite inefficient for sorting large data volumes. Bubble sort is stable and adaptive Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 4

Bubble Sort Code description: Bubble Sorting is an algorithm in which we are comparing first two values and put the larger one at higher index. Then we take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are sorted. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 5

Bubble Sort algorithm working sequence Working of bubble sort algorithm: Ας θεωρήσουμε το unsorted array A[0],A[1],A[2]... A[n-1],A[n]. Βήματα ταξινόμησης αλγόριθμου bubble sort. 1. Compare A[0] and A[1]. 2. If A[0]>A[1] then Swap A[0] and A[1]. 3. Take next A[1] and A[2]. 4. Comapre these values. 5. If A[1]>A[2] then swap A[1] and A[2] 6.... 7. at last compare A[n-1] and A[n]. 8. If A[n-1]>A[n] then swap A[n-1] and A[n]. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 6

Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 1: every step is a comparison (j=1) a. Compare first two values 12 and 9. b. 12, 9, 4, 99, 120, 1, 3, 10 c. 12>9 thus swap these values d. Then the new sequence will be e. 9, 12, 4, 99, 120, 1, 3, 10 Step 2: every step is j++ (j=2) a. take next two values 12 and 4 b. 9, 12, 4, 99, 120, 1, 3, 10 c. Compare these two values. d. 12>4 thus swap these values. e. Then the new sequence will be f. 9, 4, 12, 99, 120, 1, 3, 10 Steps 3-7: Follow similar steps up to end of array. 9 4 12 99 120 1 3 10 12<99 ok 9 4 12 99 120 1 3 10 99<120 ok 9 4 12 99 1 120 3 10 1<120 ok 9 4 12 99 1 120 3 10 120>3 swap 9 4 12 99 1 3 120 10 120>10 swap Bubble Sort Working of bubble sort algorithm Example First loop i = 0: for(i = 0; i < n; i++) public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; RESULTS new order in array = 9 4 12 99 1 3 10 120 End of the array Array is UNSORTED Start Again Second loop: for(j = 1; j < (n-i); j++) j = 3, n = 8, i = 0 (with j < (n-i) && i < n) j = 4, n = 8, i = 0 (with j < (n-i) && i < n) j = 5, n = 8, i = 0 (with j < (n-i) && i < n) j = 6, n = 8, i = 0 (with j < (n-i) && i < n) j = 7, n = 8, i = 0 (with j < (n-i) && i < n) Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 7

Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Steps 8-13: Follow similar steps up to end of array. 9 4 12 99 1 3 10 120 19>4 swap 4 9 12 99 1 3 10 120 9<12 ok 4 9 12 99 1 3 10 120 12<99 ok Bubble Sort Working of bubble sort algorithm Example 4 9 12 99 1 3 10 120 99>1 swap 4 9 12 1 99 3 10 120 99>3 swap 4 9 12 1 3 99 10 120 99>10 swap public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; First loop in code for(i = 0; i < n; i++) (with i = 1) Second loop in code for(j = 1; j < (n-i); j++) translates to for (j=1; j<7;j++) j = 1, n = 8, i = 1 (with j < (n-i) && i < n) j = 2, n = 8, i = 1 (with j < (n-i) && i < n) j = 3, n = 8, i = 1 (with j < (n-i) && i < n) j = 4, n = 8, i = 1 (with j < (n-i) && i < n) j = 5, n = 8, i = 1 (with j < (n-i) && i < n) j = 6, n = 8, i = 1 (with j < (n-i) && i < n) RESULTS (for 2 nd pass where i=1) 2 nd pass element order = 4 9 12 1 3 10 99 120 End of unsorted array reached and array is UNSORTED i++ Start Again Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 8

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 First loop in code for(i = 0; i < n; i++) (with i = 2) Steps 14-18: Follow similar steps up to end of array. 4 9 12 1 3 10 99 120 4<9 ok 4 9 12 1 3 10 99 120 9<12 ok 4 9 12 1 3 10 99 120 12>1 swap 4 9 1 12 3 10 99 120 12>3 swap 4 9 1 3 12 10 99 120 12>10 swap Second loop in code for(j = 1; j < (n-i); j++) translates to for (j=1; j<6;j++) j = 1, n = 8, i = 2 (with j < (n-i) && i < n) j = 2, n = 8, i = 2 (with j < (n-i) && i < n) j = 3, n = 8, i = 2 (with j < (n-i) && i < n) j = 4, n = 8, i = 2 (with j < (n-i) && i < n) j = 5, n = 8, i = 2 (with j < (n-i) && i < n) public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; RESULTS (for 3 rd pass where i=2) 3 rd pass element order = 4 9 1 3 10 12 99 120 End of unsorted array reached and array is UNSORTED i++ Start Again Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 9

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 First loop in code for(i = 0; i < n; i++) (with i = 3) Steps 19-22: Follow similar steps up to end of array. 4 9 1 3 10 12 99 120 4<9 ok 4 9 1 3 10 12 99 120 9>1 swap 4 1 9 3 10 12 99 120 9>3 swap 4 1 3 9 10 12 99 120 9<10 ok Second loop in code for(j = 1; j < (n-i); j++) translates to for (j=1; j<5;j++) j = 1, n = 8, i = 3 (with j < (n-i) && i < n) j = 2, n = 8, i = 3 (with j < (n-i) && i < n) j = 3, n = 8, i = 3 (with j < (n-i) && i < n) j = 4, n = 8, i = 3 (with j < (n-i) && i < n) public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; RESULTS (for 4 th pass where i=3) 4 th pass element order = 4 1 3 9 10 12 99 120 End of unsorted array reached and array is UNSORTED i++ Start Again Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 10

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 First loop in code for(i = 0; i < n; i++) (with i = 4) Steps 23-25: Follow similar steps up to end of array. 4 1 3 9 10 12 99 120 4>1 swap 1 4 3 9 10 12 99 120 4>3 swap 1 3 4 9 10 12 99 120 4<9 ok Second loop in code for(j = 1; j < (n-i); j++) translates to for (j=1; j<4;j++) j = 1, n = 8, i = 4 (with j < (n-i) && i < n) j = 2, n = 8, i = 4 (with j < (n-i) && i < n) j = 3, n = 8, i = 4 (with j < (n-i) && i < n) public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; RESULTS (for 5 th pass where i=4) 5 th pass element order = 1 3 4 9 10 12 99 120 End of unsorted array reached and array is UNSORTED i++ Start Again Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 11

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 First loop in code for(i = 0; i < n; i++) (with i = 5) Steps 26-27: Follow similar steps up to end of array. 1 3 4 9 10 12 99 120 1<3 ok 1 3 4 9 10 12 99 120 3<4 ok Second loop in code for(j = 1; j < (n-i); j++) translates to for (j=1; j<3;j++) j = 1, n = 8, i = 5 (with j < (n-i) && i < n) j = 2, n = 8, i = 5 (with j < (n-i) && i < n) public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; RESULTS (for 6 th pass where i=5) 6 th pass element order = 1 3 4 9 10 12 99 120 End of unsorted array reached and array is UNSORTED i++ Start Again Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 12

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 First loop in code for(i = 0; i < n; i++) (with i = 6) Steps 28: Follow similar steps up to end of array. 1 3 4 9 10 12 99 120 1<3 ok Second loop in code for(j = 1; j < (n-i); j++) translates to for (j=1; j<2;j++) j = 1, n = 8, i = 6 (with j < (n-i) && i < n) public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; RESULTS (for 7 th pass where i=6) 7 th pass element order = 1 3 4 9 10 12 99 120 End of unsorted array reached and array is UNSORTED i++ Start Again Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 13

Bubble Sort Working of bubble sort algorithm Example Example array values: 12, 9, 4, 99, 120, 1, 3, 10 Steps 29 and 30 do not change anything in the arrays, so they don t count as COMPARES. They are just to demonstrate the function of the outer FOR loop and the exit of the bubble_srt( ) function. Step 29: No comparison in this step j = 1 n = 8 i = 7 with j < (n-i) && i < n; In every end of array i++ ; Step 30: No comparison in this step j = 1 n = 8 i = 8 with j < (n-i) && i < n; Now it exits the function Sorted array values: 1, 3, 4, 9, 10, 12, 99, 120, Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 14

Turtles and rabbits Bubble Sort One more problem of bubble sort is that its running time badly depends on the initial order of the elements. Big elements (rabbits) go up fast, while small ones (turtles) go down very slow. This problem is solved in the Cocktail sort. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 15

Bubble Sort Working of bubble sort algorithm Turtle Example Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 16

Bubble Sort Working of bubble sort algorithm Rabbit Example Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 17

Bubble Sort Example code in Java public class bubblesort{ public static void main(string a[]){ int i; int array[] = {12,9,4,99,120,1,3,10; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 18

Bubble Sort Example code in C++ public class bubblesort{ void bubblesort(int arr[], int n) { bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < n - j; i++) { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; swapped = true; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 19

Bubble Sort Example code in C /* Bubble sort code */ #include <stdio.h> main() { int array[100], n, c, d, swap; printf("enter number of elements\n"); scanf("%d", &n); printf("enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; printf("sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); Copyrights: http://www.programmingsimplified.com/c/source-code/cprogram-bubble-sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 20

Bidirectional Bubble Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 21

Bidirectional Bubble Sort Introduction: Bi-directional bubble sorting also known as cocktail shaker sort, shaker sort, double-direction bubble sort. Facts: A alternative of bubble sort is bi-directional bubble sort. The bi-directional bubble sort compares each adjacent pair of elements in an array. The values will be swap if necessary. The values passes from the beginning to the end and also from the end to the beginning. It stops when there is no any element to swap. The complexity of bi-directional bubble sort is O(n2). Bi-directional bubble sort is better than bubble sort. In Bi-directional bubble sort at least one elements is moved forward or backward to its place in the array with each pass. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 22

Bidirectional Bubble Sort algorithm working sequence Working of bubble sort algorithm: Ας θεωρήσουμε το unsorted array A[0],A[1],A[2]... A[n-1],A[n]. Βήματα ταξινόμησης αλγόριθμου bubble sort. 1. Compare A[0] &A[1] and A[n-1] & A[n]. 2. If A[0]>A[1] then Swap A[0] & A[1] and also if A[n-1]>A[n] then swap it. 3. Take next A[1] & A[2] and A[n-2] & A[n-1]. 4. Comapre these values. 5. If A[1]>A[2] then Swap A[1] & A[2] and also if A[n-2]>A[n-1] then swap it. 6.... 7.... Stop: when there is no any pass for swap or the array values are in sorted order. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 23

Bidirectional Bubble Sort public class BidirectionalBubbleSort{ public static void main(string a[]){ int i; int array[] = {12,9,4,99,120,1,3,10; System.out.println("\n\n RoseIndia\n\n"); System.out.println(" Bidirectional Bubble Sort\n\n"); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bidirectionalbubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); Example code public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 24

Bidirectional Bubble Sort Παράδειγμα σε C /* * C Program to Implement CockTail Sort */ #include <stdio.h> #define MAX 8 int main() { int data[max]; int i, j, n, c; printf("\nenter the data"); for (i = 0; i < MAX; i++) { scanf("%d", &data[i]); n = MAX; do { /* * Rightward pass will shift the largest element to its correct place at the end */ for (i = 0; i < n - 1; i++) { if (data[i] > data[i + 1]) { data[i] = data[i] + data[i + 1]; data[i + 1] = data[i] - data[i + 1]; data[i] = data[i] - data[i + 1]; n = n - 1; /* * Leftward pass will shift the smallest element to its correct place at the beginning */ for (i= MAX - 1, c = 0; i >= c; i--) { if(data[i] < data[i - 1]) { data[i] = data[i] + data[i - 1]; data[i - 1] = data[i] - data[i - 1]; data[i] = data[i] - data[i - 1]; c = c + 1; while (n!= 0 && c!= 0); printf("the sorted elements are:"); for (i = 0; i < MAX; i++) { printf("%d\t", data[i]); Copyrights: http://www.sanfoundry.com/c-program-implement-cocktail-sort/ Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 25

Bidirectional Bubble Sort Working of bubble sort algorithm Example Example array values: 12, 9, 4, 99, 120, 1, 3, 10 Steps 1-7: Start to End Direction 12 9 4 99 120 1 3 10 12>9 swap 9 12 4 99 120 1 3 10 12>4 swap 9 4 12 99 120 1 3 10 12<99 ok 9 4 12 99 120 1 3 10 99<120 ok 9 4 12 99 120 1 3 10 120>1 swap 9 4 12 99 1 120 3 10 120>3 swap 9 4 12 99 1 3 120 10 120>10 swap new temp array = 9 4 12 99 1 3 10 120 Steps 7-14: End to Start Direction 9 4 12 99 1 3 10 120 10<120 ok 9 4 12 99 1 3 10 120 3<10 ok 9 4 12 99 1 3 10 120 1<3 ok 9 4 12 99 1 3 10 120 99>1 swap 9 4 12 1 99 3 10 120 12>1 swap 9 4 1 12 99 3 10 120 4>1 swap 9 1 4 12 99 3 10 120 9>1 swap Steps 15-19: Start to End Direction 1 9 4 12 99 3 10 120 9>4 swap 1 4 9 12 99 3 10 120 9>12 ok 1 4 9 12 99 3 10 120 12>99 ok 1 4 9 12 99 3 10 120 99>3 swap 1 4 9 12 3 99 10 120 99>10 swap new temp array = 1 4 9 12 3 10 99 120 Steps 20-24: End to Start Direction 1 4 9 12 3 10 99 120 10<99 ok 1 4 9 12 3 10 99 120 3<10 ok 1 4 9 12 3 10 99 120 12>3 swap 1 4 9 3 12 10 99 120 9>3 swap 1 4 3 9 12 10 99 120 4>3 swap new array = 1 3 4 9 12 10 99 120 new array = 1 9 4 12 99 3 10 120 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 26

Bidirectional Bubble Sort Working of bubble sort algorithm Example Example array values: 12, 9, 4, 99, 120, 1, 3, 10 Steps 25-27: Start to End Direction 1 3 4 9 12 10 99 120 4<9 ok 1 3 4 9 12 10 99 120 9<12 ok 1 3 4 9 12 10 99 120 12>10 swap New temp array = 1 3 4 9 10 12 99 120 Steps 28-30: End to Start Direction 1 3 4 9 10 12 99 120 10<12 ok 1 3 4 9 10 12 99 120 9<10 ok 1 3 4 9 10 12 99 120 4<9 ok new array = 1 3 4 9 10 12 99 120 Step 31: Start to End Direction 1 3 4 9 10 12 99 120 9<10 ok new temp array = 1 3 4 9 10 12 99 120 Step 32: End to Start Direction 1 3 4 9 10 12 99 120 9<12 ok new array = 1 3 4 9 10 12 99 120 sorted So we have 32 steps Sorted array values: 1, 3, 4, 9, 10, 12, 99, 120, Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 27

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Begin the bidirectionalbubble_srt() with st = -1 Before Step1 and before entering the FOR loops, it checks the WHILE statement. Then it adds 1 to the st variable. So st = 0; And it subtracks 1 from the n variable. So n = 7 Steps 1-7: Start to End Direction j = st j = 0; so it starts comparing from: array[0] 12 9 4 99 120 1 3 10 12>9 swap 9 12 4 99 120 1 3 10 12>4 swap 9 4 12 99 120 1 3 10 12<99 ok 9 4 12 99 120 1 3 10 99<120 ok 9 4 12 99 120 1 3 10 120>1 swap 9 4 12 99 1 120 3 10 120>3 swap 9 4 12 99 1 3 120 10 120>10 swap And stop after 7 loops because j must be < than n so j < 7 new temp array: 9 4 12 99 1 3 10 120 NEXT 2 PAGES STEP 1 4 WITH DETAILES public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 28

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 1: From Start To End. We are in 1 st FOR loop with n=7, st=0, j=0 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[0]>Array[1] Inside the IF it SWAPS the values of the array. 12 9 4 99 120 1 3 10 12>9 swap j++; New Array: 9 12 4 99 120 1 3 10 This loops for j<n, which means j < 7. Step 2: From Start To End. We are in 1 st FOR loop with n=7, st=0, j=1 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[1]>Array[2] Inside the IF it SWAPS the values of the array. 9 12 4 99 120 1 3 10 12>4 swap j++; New Array: 9 4 12 99 120 1 3 10 This loops for j<n, which means j < 7. public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 29

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 3: From Start To End. We are in 1 st FOR loop with n=7, st=0, j=2 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[2]>Array[3] Inside the IF it SWAPS the values of the array. 9 4 12 99 120 1 3 10 12<99 ok j++; New Array: 9 4 12 99 120 1 3 10 This loops for j<n, which means j < 7. Step 4: From Start To End. We are in 1 st FOR loop with n=7, st=0, j=3 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[3]>Array[4] Inside the IF it SWAPS the values of the array. 9 4 12 99 120 1 3 10 99<120 ok j++; New Array: 9 4 12 99 120 1 3 10 This loops for j<n, which means j < 7. SAME STEPS FOR J < N public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 30

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 st = 0; n = 7 First subtrack 1 from j then check if j >= st So, the starting value of j (j = n) will NOT be 7 but 6. Steps 8-14: End to Start Direction j=n j 7 but j j=6; so it starts comparing from array[6] > array[7] 9 4 12 99 1 3 10 120 10<120 ok j=6 9 4 12 99 1 3 10 120 3<10 ok j=5 9 4 12 99 1 3 10 120 1<3 ok j=4 9 4 12 99 1 3 10 120 99>1 swap j=3 9 4 12 1 99 3 10 120 12>1 swap j=2 9 4 1 12 99 3 10 120 4>1 swap j=1 9 1 4 12 99 3 10 120 9>1 swap j=0 And stops after 7 loops because j >= st; j >= 0; new array = 1 9 4 12 99 3 10 120 NEXT 2 PAGES STEP 8 11 WITH DETAILES public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 31

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 8: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=7, j=n, st=0, j=6 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[6]>Array[7] Inside the IF it SWAPS the values of the array. 9 4 12 99 1 3 10 120 10<120 ok New Array: 9 4 12 99 1 3 10 120 This loops for j>=st, which means j >= 0. Step 9: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=7, st=0, j=5 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[5]>Array[6] Inside the IF it SWAPS the values of the array. 9 4 12 99 1 3 10 120 3<10 ok New Array: 9 4 12 99 1 3 10 120 This loops for j>=st, which means j >= 0. public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 32

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 10: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=7, st=0, j=4 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[4]>Array[5] Inside the IF it SWAPS the values of the array. 9 4 12 99 1 3 10 120 1 < 3 ok New Array: 9 4 12 99 1 3 10 120 This loops for j>=st, which means j >= 0. Step 11: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=7, st=0, j=3 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[3]>Array[4] Inside the IF it SWAPS the values of the array. 9 4 12 99 1 3 10 120 99 > 1 swap New Array: 9 4 12 1 99 3 10 120 This loops for j>=st, which means j >= 0. SAME STEPS FOR J >= ST public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 33

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 At Step14 both FOR loops are completed. So it checks the WHILE statement again. Then it adds 1 to the st variable. So st = 1; And it subtracks 1 from the n variable. So n = 6 Steps 15-19: Start to End Direction j = st j = 1; so it starts comparing from: array[1] 1 9 4 12 99 3 10 120 9>4 swap 1 4 9 12 99 3 10 120 9>12 ok 1 4 9 12 99 3 10 120 12>99 ok 1 4 9 12 99 3 10 120 99>3 swap 1 4 9 12 3 99 10 120 99>10 swap And stop after 5 loops because j must be < than n so j < 6 new temp array: 1 4 9 12 3 10 99 120 NEXT 2 PAGES STEP 15 18 WITH DETAILES public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 34

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 15: From Start To End. We are in 1 st FOR loop with n=6, st=1, j=1 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[1]>Array[2] Inside the IF it SWAPS the values of the array. 1 9 4 12 99 3 10 120 9>4 swap j++; New Array: 1 4 9 12 99 3 10 120 This loops for j<n, which means j < 6. Step 16: From Start To End. We are in 1 st FOR loop with n=6, st=1, j=2 On the IF we COMPARES the elements of the array in couples.the current and the next one. Array[2]>Array[3] Inside the IF it SWAPS the values of the array. 1 4 9 12 99 3 10 120 9<12 ok j++; New Array: 1 4 9 12 99 3 10 120 This loops for j<n, which means j < 6. public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 35

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 17: From Start To End. We are in 1 st FOR loop with n=6, st=1, j=3 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[3]>Array[4] Inside the IF it SWAPS the values of the array. 1 4 9 12 99 3 10 120 12<99 ok j++; New Array: 1 4 9 12 99 3 10 120 This loops for j<n, which means j < 6. Step 18: From Start To End. We are in 1 st FOR loop with n=6, st=1, j=4 On the IF we COMPARES the elements of the array in couples.the current and the next one. Array[4]>Array[5] Inside the IF it SWAPS the values of the array. 1 4 9 12 99 3 10 120 99>3 swap j++; New Array: 1 4 9 12 3 99 10 120 This loops for j<n, which means j < 6. SAME STEPS FOR J < N public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 36

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 st = 1; n = 6 First subtrack 1 from j then check if j >= st So, the starting value of j (j = n) will NOT be 6 but 5. Steps 20-24: End to Start Direction j=n j 6 but j j=5; so it starts comparing from array[5] > array[6] 1 4 9 12 3 10 99 120 10<99 ok j = 5 1 4 9 12 3 10 99 120 3<10 ok j = 4 1 4 9 12 3 10 99 120 12>3 swap j = 3 1 4 9 3 12 10 99 120 9>3 swap j = 2 1 4 3 9 12 10 99 120 4>3 swap j = 1 And stops after 5 loops because j >= st; j >= 1; new array = 1 3 4 9 12 10 99 120 NEXT 2 PAGES STEP 20 23 WITH DETAILES public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 37

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 20: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=6, st=1, j=5 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[5]>Array[6] Inside the IF it SWAPS the values of the array. 1 4 9 12 3 10 99 120 10<99 ok j++; New Array: 1 4 9 12 3 10 99 120 This loops for j>=st, which means j >= 1. Step 21: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=6, st=1, j=4 On the IF we COMPARES the elements of the array in couples.the current and the next one. Array[4]>Array[5] Inside the IF it SWAPS the values of the array. 1 4 9 12 3 10 99 120 3<10 ok j++; New Array: 1 4 9 12 3 10 99 120 This loops for j>=st, which means j >= 1. public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 38

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 22: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=6, st=1, j=3 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[3]>Array[4] Inside the IF it SWAPS the values of the array. 1 4 9 12 3 10 99 120 12>3 swap j++; New Array: 1 4 9 3 12 10 99 120 This loops for j>=st, which means j >= 1. Step 23: From End To Start. Here it first decreases the j and then enters the loop.(--j) We are in 2 st FOR loop with n=6, st=1, j=2 On the IF we COMPARES the elements of the array in couples.the current and the next one. Array[2]>Array[3] Inside the IF it SWAPS the values of the array. 1 4 9 3 12 10 99 120 9>3 swap j++; New Array: 1 4 3 9 12 10 99 120 This loops for j>=st, which means j >= 1. public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 39

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 At Step24 both FOR loops are completed again. So it checks the WHILE statement once more. Then it adds 1 to the st variable. So st = 2; And it subtracks 1 from the n variable. So n = 5 Steps 25-27: Start to End Direction j = st => j = 2; so it starts comparing from: array[2] 1 3 4 9 12 10 99 120 4<9 ok 1 3 4 9 12 10 99 120 9<12 ok 1 3 4 9 12 10 99 120 12>10 swap And stop after 3 loops because j must be < than n so j < 5 new temp array: 1 3 4 9 10 12 99 120 NEXT 2 PAGES STEP 25 27 WITH DETAILES public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 40

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 25: From Start To End. We are in 1 st FOR loop with n=5, st=2, j=2 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[2]>Array[3] Inside the IF it SWAPS the values of the array. 1 3 4 9 12 10 99 120 4<9 ok j++; New Array: 1 3 4 9 12 10 99 120 This loops for j<n, which means j < 5. Step 26: From Start To End. We are in 1 st FOR loop with n=5, st=2, j=3 On the IF we COMPARES the elements of the array in couples.the current and the next one. Array[3]>Array[4] Inside the IF it SWAPS the values of the array. 1 3 4 9 12 10 99 120 9<12 ok j++; New Array: 1 3 4 9 12 10 99 120 This loops for j<n, which means j < 5. public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 41

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 Step 27: From Start To End. We are in 1 st FOR loop with n=5, st=2, j=4 On the IF it COMPARES the elements of the array in couples.the current and the next one. Array[3]>Array[4] Inside the IF it SWAPS the values of the array. 1 3 4 9 12 10 99 120 12>10 swap j++; New Array: 1 3 4 9 10 12 99 120 This loops for j<n, which means j < 5. SAME STEPS FOR J < N public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 42

Bubble Sort Working of bubble sort algorithm Example Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 st = 2; n = 5; First subtrack 1 from j then check if j >= st So, the starting value of j (j = n) will NOT be 5 but 4. Steps 28-30: End to Start Direction 1 3 4 9 10 12 99 120 10<12 ok 1 3 4 9 10 12 99 120 9<10 ok 1 3 4 9 10 12 99 120 4<9 ok new array = 1 3 4 9 12 10 99 120 NEXT PAGE STEP 28 30 WITH DETAILES public static void bidirectionalbubble_srt(int array[], int n){ int j; int st = -1; while (st < n) { st++; n--; for (j = st; j < n; j++) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; for (j = n; --j >= st;) { if (array[j] > array[j + 1]) { int T = array[j]; array[j] = array[j + 1]; array[j + 1] = T; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 43

Quick Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 44

Quick sort Introduction Quick sort algorithm is developed by C. A. R. Hoare. Quick sort is a comparison sort. The working of quick sort algorithm is depending on a divide-and-conquer strategy. A divide and conquer strategy is dividing an array into two sub-arrays. Quick sort is one of the fastest and simplest sorting algorithm. The complexity of quick sort in the average case is Θ(n log(n)) and in the worst case is Θ(n2). Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 45

Quick sort Code description In quick sort algorithm pick an element from array of elements. This element is called the pivot. Then compare the values from left to right until a greater element is find then swap the values. Again start comparison from right with pivot. When lesser element is find then swap the values. Follow the same steps until all elements which are less than the pivot come before the pivot and all elements greater than the pivot come after it. After this partitioning, the pivot is in its last position.this is called the partition operation. Recursively sort the sub-array of lesser elements and the sub-array of greater elements. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 46

Quick sort Algorithm description The divide-and-conquer strategy is used in quick-sort. Below the recursion step is described: Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array. Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts. Sort both parts. Apply quick-sort algorithm recursively to the left and the right parts. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 47

Quick sort Partition algorithm in detail There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an element with value greater or equal to the pivot is found. Index j is moved backward, until an element with value lesser or equal to the pivot is found. If i j then they are swapped and i steps to the next position (i + 1), j steps to the previous one (j - 1). Algorithm stops, when i becomes greater than j. After partition, all values before i-th element are less or equal than the pivot and all values after j-th element are greater or equal to the pivot. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 48

Quick Sort An example Example. Sort {1, 12, 5, 26, 7, 14, 3, 7, 2 using quicksort. Notice, that we show here only the first recursion step, in order not to make example too long. But, in fact, {1, 2, 5, 7, 3 and {14, 7, 26, 12 are sorted then recursively. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 49

Quick Sort Working of quick sort algorithm Example Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 50

Quick Sort Working of quick sort algorithm Example Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 51

Quick Sort Working of quick sort algorithm Example Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 52

Quick Sort Example code1 public class QuickSort{ public static void main(string a[]){ int i; int array[] = {12,9,4,99,120,1,3,10,13; System.out.println("\n\n RoseIndia\n\n"); System.out.println(" Quick Sort\n\n"); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); quick_srt(array,0,array.length-1); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); public static void quick_srt(int array[],int low, int n){ int lo = low; int hi = n; if (lo >= n) { return; int mid = array[(lo + hi) / 2]; while (lo < hi) { while (lo<hi && array[lo] < mid) { lo++; while (lo<hi && array[hi] > mid) { hi--; if (lo < hi) { int T = array[lo]; array[lo] = array[hi]; array[hi] = T; if (hi < lo) { int T = hi; hi = lo; lo = T; quick_srt(array, low, lo); quick_srt(array, lo == low? lo+1 : lo, n); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 53

Quick Sort Example code_2 public class QuickSort{ public static void main(string a[]){ int i; int array[] = {12,9,4,99,120,1,3,10,13; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); quick_srt(array,0,array.length-1); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); public static void quick_srt(int array[],int low, int high){ int lq = low; int hq = high; int mid, temp; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; if(lq <= hq){ temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(lq <= hq); if(low < hq) quick_srt(array, low, hq); if(lq < high) quick_srt(array, lq, high); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 54

Step By Step Analisys(Theory) Array Elements: Start Order = 12,9,4,99,120,1,3,10,13 It originates values of low and high into the others because we are going to use it again so we need them stored somewhere. It also assigns to mid the value stored into the array we need to be our pivot. Now we enter the do loop and the firtst while loop. Here it checks whether the array[lq] is smaller than mid. While this is true it adds 1 to the lq variable. When the first while loop ends, it enters the second: Here it checks whether the mid is smaller than the array[hq]. While this is true it subtracks 1 from the hq variable. In both of the while loops is where COMPARISONS take place. In this 1 st if statement, the SWAPS are done. All the above run while the do-while loop is true. (lq < = hq) Now we come to the last two if statements. On the 2 nd if it checks if low, the one we had from the beginnig(low = 0) is smaller than the hq the one that changed through the quick(). If its true it calls itself with array, low, hq as definitions. Same thing will happen on the 3 rd if but with different variables for the definition. If none of the two if is true it ends the quick algorithm and returns to main. public static void quick(int array[],int low, int high){ int mid, temp; int lq = low; int hq = high; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; if(lq <= hq){ temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(loq <= hq); if(low < hq) quick(array, low, hq); if(lq < high) quick(array, lq, high); 1 st While 2 nd While Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 55 1 st If 2 nd if 3 rd if

Step By Step Analisys(Variables) Array Elements: Start Order = 12,9,4,99,120,1,3,10,13 VALUES FOR FISRT RUN OF quick() array[] = {12,9,4,99,120,1,3,10,13, low = 0, high = 8 because (array.length 1); lq = 0; hq = 8; mid = array[ ( 0+8) / 2] = array[4] = 120; array[0] = 12, mid = 120 12 < 120 true lq++; array[1] = 9, mid = 120 9 < 120 true lq++; array[2] = 4, mid = 120 4 < 120 true lq++; array[3] = 99, mid = 120 99 < 120 true lq++; array[4] = 120,mid = 120 120 < 120 false ------ (lq=4) array[8] = 13,mid = 120 120 < 13 false ------ (hq=8) lq = 4,hq = 8 4 < = 8 true temp = array[4]; temp = 120 array[4] = array[8]; array[4] = 13 array[8] = temp; array[8] = 120 lq++; lq = 5 hq--; hq = 7 new array: {12, 9, 4, 99, 13, 1, 3, 10, 120; lq = 5, hq = 7 5 < = 7 true run again public static void quick(int array[],int low, int high){ int mid, temp; int lq = low; int hq = high; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; Runs 4 times Runs 0 times if(lq <= hq){ Enters temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(lq <= hq); It s true and runs again if(low < hq) quick(array, low, hq); if(lq < high) quick(array, lq, high); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 56

Step By Step Analisys(Variables) Array Elements: Start Order = 12,9,4,99,120,1,3,10,13 public static void quick(int array[],int low, int high){ Having from previous: lq = 5; hq = 7; mid = 120 array[ ] = {12, 9, 4, 99, 13, 1, 3, 10, 120; array[5] = 1, mid = 120 1 < 120 true lq++; array[6] = 3, mid = 120 3 < 120 true lq++; array[7] = 10, mid = 120 10 < 120 true lq++; array[8] = 120, mid = 120 120 < 120 false ---- (lq=8) array[7] = 10, mid = 120 120 < 10 false --- (hq = 7) int mid, temp; int lq = low; int hq = high; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; Runs 3 times Runs 0 times lq = 8, hq = 7 8 <= 7 false new array: {12, 9, 4, 99, 13, 1, 3, 10, 120; same as before lq = 8, hq = 7 8 < = 7 false low = 0, hq = 7 0 < 7 true array[ ] = {12, 9, 4, 99, 13, 1, 3, 10, 120, low = 0, hq = 7; Now it transfers these values and the array back to the top of the algorithm. So at the next step we ll see how it s done. if(lq <= hq){ Doesn t Enter temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(lq <= hq); Has run 2 times if(low < hq) quick(array, low, hq); if(lq < high) quick(array, lq, high); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 57

Step By Step Analisys(Variables) Array Elements: Start Order = 12,9,4,99,120,1,3,10,13 VALUES FOR SECOND RUN OF quick() new array: {12, 9, 4, 99, 13, 1, 3, 10, 120, low = 0, high = 7 because the values are transferred from the previous run of quick() when we called quick(array, low, hq); lq = 0; hq = 7; mid = array[ ( 0+7) / 2] = array[4] = 13; array[0] = 12, mid = 13 12 < 13 true lq++; array[1] = 9, mid = 13 9 < 13 true lq++; array[2] = 4, mid = 13 4 < 13 true lq++; array[3] = 99, mid = 13 99 < 13 false ---- (lq = 3) public static void quick(int array[],int low, int high){ int mid, temp; int lq = low; int hq = high; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; Runs 3 times Runs 0 times array[7] = 10, mid = 13 13 < 10 false ---- (hq = 7) lq = 3, hq = 7 3 <= 7 true temp = array[3]; temp = 99 array[3] = array[7]; array[3] = 10 array[7] = temp; array[7] = 99 lq++; lq = 4 hg--; hq = 6 new array: {12, 9, 4, 10, 13, 1, 3, 99, 120; if(lq <= hq){ Enters temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(lq <= hq); It s true and runs again lq = 4, hq = 6 4 < = 6 true run again if(low < hq) quick(array, low, hq); if(lq < high) quick(array, lq, high); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 58

Step By Step Analisys(Variables) Array Elements: Start Order = 12,9,4,99,120,1,3,10,13 public static void quick(int array[],int low, int high){ Having from previous: lq = 4; hq = 6; mid = 13 array[ ] = {12, 9, 4, 10, 13, 1, 3, 99, 120; array[4] = 13, mid = 13 13 < 13 false ---- (lq = 4) array[6] = 3, mid = 13 13 < 3 false ----(hq = 6) int mid, temp; int lq = low; int hq = high; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; Runs 0 times Runs 0 times lq = 4, hq = 6 4 <= 6 true temp = array[4]; temp = 13 array[4] = array[6]; array[4] = 3 array[6] = temp; array[6] = 13 lq++; lq = 5 hq--; hq = 5 new array: {12, 9, 4, 10, 3, 1, 13, 99, 120; lq = 5, hq = 5 5 < = 5 true run again if(lq <= hq){ Enters temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(lq <= hq); It s true and runs again if(low < hq) quick(array, low, hq); if(lq < high) quick(array, lq, high); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 59

Step By Step Analisys(Variables) Array Elements: Start Order = 12,9,4,99,120,1,3,10,13 public static void quick(int array[],int low, int high){ Having from previous: lq = 5; hq = 5; mid = 13 array[ ] = {12, 9, 4, 10, 3, 1, 13, 99, 120; array[5] = 1, mid = 13 1 < 13 true lq++; array[6] = 13, mid = 13 13 < 13 false ----- (lq = 6) array[5] = 1, mid = 13 13 < 1 false ---- (hq = 5) lq = 6, hq = 5 6 <= 5 false new array: {12, 9, 4, 10, 3, 1, 13, 99, 120; same as before lq = 6, hq = 5 6 < = 5 false low = 0, hq = 5 0 < 5 true array[ ] = {12, 9, 4, 10, 3, 1, 13, 99, 120, low = 0, hq = 5; Now it transfers these values and the array back to the top of the algorithm. So at the next step we ll see how it s done. int mid, temp; int lq = low; int hq = high; mid = array[(low + high)/2]; do{ while(array[lq] < mid) lq++; while(mid < array[hq]) hq--; if(lq <= hq){ temp = array[lq]; array[lq] = array[hq]; array[hq] = temp; lq++; hq--; while(lq <= hq); if(low < hq) quick(array, low, hq); if(lq < high) quick(array, lq, high); Runs 1 time Runs 0 times Doesn t Enters Has run 3 times Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 60