© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Chapter 2.9 Sorting Arrays. Sort Algorithms A set of records is given Each record is identified by a certain key One wants to sort the records according.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
CSE 373: Data Structures and Algorithms
Simple Sorting Algorithms
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSE 373 Data Structures and Algorithms
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Sort Algorithms.
Sorting Dr. Yingwu Zhu. Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order Ascending or descending Some O(n.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Sorting Lesson CS1313 Spring Sorting Lesson Outline 1.Sorting Lesson Outline 2.How to Sort an Array? 3.Many Sorting Algorithms 4.Bubble Sort #1.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Sort Algorithm.
Sorting Dr. Yingwu Zhu.
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Algorithms
Introduction to Search Algorithms
Sorting Algorithms.
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
CSC215 Lecture Algorithms.
Sorting Dr. Yingwu Zhu.
Simple Sorting Algorithms
Simple Sorting Algorithms
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching

© Janice Regan, CMPT 102, Sept Putting numbers into order  There are many approaches to taking an array of numbers and putting those numbers into ascending or descending order.  Insertion Sort (see lab 6)  Selection Sort  Bubble Sort  Quick Sort

© Janice Regan, CMPT 102, Sept Ascending / Descending Order Unsorted Array A[13] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Sorted Array A[13]: Descending order A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Sorted Array A[13]: Ascending order A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12]

© Janice Regan, CMPT 102, Sept Selection Sort  A method to transform the unsorted array into a sorted array. This example sorts into ascending order, sorting into descending order is a parallel process.  Apply a simple algorithm to an array of length M  Start with N=M  Consider the last N elements in the array, A[M-N] to A[M]  Find the element that contains the smallest value in the group of elements defined in (2). Record the index I at which this smallest value is found. Smallest = A[i]  Swap the smallest value with the (M-N)th element A[M-N] swaped with A[i] the smallest element  Decrease N by 1 and return to step 2 (until N<0)

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 1 Unsorted Array A[13] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[1], switch with A[M-N]=A[0] Iteration 1: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 1: M=N

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 2 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[10], switch with A[M-N]=A[1] Iteration 2: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 2: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[4], switch with A[M-N]=A[2] Iteration 3: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 3: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 4 After Iteration 4: N=M-3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[4], switch with A[M-N]=A[3] Iteration 4: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12]

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 5 After Iteration 5: N=M-4 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[11], switch with A[M-N]=A[4] Iteration 5: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12]

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 6 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[11], switch with A[M-N]=A[5] Iteration 6: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 6: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 7 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[10], switch with A[M-N]=A[6] Iteration 7: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 7: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[12], switch with A[M-N]=A[7] Iteration 8: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 8: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 9 Smallest element is A[8], switch with A[M-N]=A[8] Iteration 9: N=M-8 After Iteration 9: N=M-8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12]

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 10 A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] Smallest element is A[10], switch with A[M-N]=A[9] Iteration 10: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 10: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 11 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[11], switch with A[M-N]=A[10] Iteration 11: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 11: N=M

© Janice Regan, CMPT 102, Sept Sample Selection Sort: 12 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[12], switch with A[M-N]=A[12] Iteration 12: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 13: N=M

© Janice Regan, CMPT 102, Sept Selection sort function

© Janice Regan, CMPT 102, Sept Bubble Sort  A method to transform the unsorted array into a sorted array. This example sorts into ascending order, sorting into descending order is a parallel process.  Apply a simple algorithm to an array of length M  Start with N=0  Consider the array elements A[0] to A[M-1-N]  For each I, 0 <= i <M-1-N  Compare A[i] and A[i+1] and put in order  A[M-1-N] now contains the largest number, it is in order  Increase N by 1 and return to step 2 (until N=M-1)

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 1 Compare A[M-N+5] and A[M-N+6], put in order A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N] and A[M-N+I], put in order Iteration 1: N=M Compare A[M-N+1] and A[M-N+2], put in order Compare A[M-N+2] and A[M-N+3], put in order Compare A[M-N+3] and A[M-N+4], put in order 7 Compare A[M-N+4] and A[M-N+5], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 2 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Compare A[M-N+9] and A[M-N+10], put in order Compare A[M-N+10] and A[M-N+11], put in order Compare A[M-N+11] and A[M-N+12], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N] and A[M-N+I], put in order Iteration 2: N=M Compare A[M-N+1] and A[M-N+2], put in order Compare A[M-N+2] and A[M-N+3], put in order Compare A[M-N+3] and A[M-N+4], put in order 19 Compare A[M-N+4] and A[M-N+5], put in order Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 4 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Compare A[M-N+9] and A[M-N+10], put in order Compare A[M-N+9] and A[M-N+10], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 5 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N] and A[M-N+I], put in order Iteration 3: N=M Compare A[M-N+1] and A[M-N+2], put in order Compare A[M-N+2] and A[M-N+3], put in order Compare A[M-N+3] and A[M-N+4], put in order 19 Compare A[M-N+4] and A[M-N+5], put in order Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 6 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Compare A[M-N+9] and A[M-N+10], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 7 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Iteration 4: N=M-3 No changes in first 6 comparisons then continue with

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Iteration 5: N=M-4 No changes in first 5 comparisons then continue with Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 9 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+4] and A[M-N+5], put in order Iteration 6: N=M-5 No changes in first 4 comparisons then continue with Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 10 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+3] and A[M-N+4], put in order Compare A[M-N+4] and A[M-N+5], put in order Iteration 7: N=M-6 No changes in first 3 comparisons then continue with Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 11 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+3] and A[M-N+4], put in order Compare A[M-N+4] and A[M-N+5], put in order Iteration 8: N=M-7 No changes in first 3 comparisons then continue with Compare A[M-N+2] and A[M-N+3], put in order

© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 12 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+3] and A[M-N+4], put in order Compare A[M-N+1] and A[M-N+2], put in order Iteration 9: N=M-8 No changes in first 3 comparisons then continue with Compare A[M-N+2] and A[M-N+3], put in order All numbers are in order: We are done

© Janice Regan, CMPT 102, Sept Bubble Sort Function void BubbleSort(int InArray[], int M, int N) { int end; int j; int k; int temp; int sorted=FALSE; end = N; while(!sorted) { sorted = TRUE; for( k=M; k<end; k++) { if(InArray[k] > InArray[k+1] ) { sorted = FALSE; temp = InArray[k]; InArray[k] = InArray[k+1]; InArray[k+1] = temp; } } end--; }

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 1

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 2 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Pivot More than pivot Pivot More than pivot Pivot More than pivotLess than pivot First element smaller than pivot Pivot Less than pivot More than pivot Pivot Less than pivot First element larger than pivot First element smaller than pivot

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Less than pivot Pivot, middle element More than pivot Pivot Less than pivot First element larger than pivot More than pivot Pivot Less than pivot First element smaller than pivot More than pivot Pivot Less than pivot

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 4 First element smaller than pivot

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 5 First element larger than pivot A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Less than pivot Pivot More than pivot Pivot, middle element Pivot

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 6

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 7

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] Pivot Pivot

© Janice Regan, CMPT 102, Sept Sample Quick Sort: 9 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Pivot Pivot Pivot First element larger than pivot

© Janice Regan, CMPT 102, Sept Quicksort function: 1 void Quicksort( int InArray[], int loBound, int hiBound ) { int pivot; int loSwap; int hiSwap; int temp; /* Zero or one item to sort */ if (loBound >= hiBound) { return; } /* Two items to sort */ if (hiBound-loBound == 1) { if (InArray[loBound] > InArray[hiBound]) { temp = InArray[loBound]; InArray[loBound] = InArray[hiBound]; InArray[hiBound] = temp; } return; }

© Janice Regan, CMPT 102, Sept Quicksort function: 2 /* 3 or more items to sort */ pivot = InArray[(loBound+hiBound)/2]; InArray[(loBound+hiBound)/2] = InArray[loBound]; InArray[loBound] = pivot; loSwap = loBound + 1; hiSwap = hiBound;

© Janice Regan, CMPT 102, Sept Quicksort function: 3 do { while (loSwap <= hiSwap && InArray[loSwap] <= pivot) { loSwap++; } while (InArray[hiSwap] > pivot) { hiSwap--; } if (loSwap < hiSwap) { temp = InArray[loSwap]; InArray[loSwap] = InArray[hiSwap]; InArray[hiSwap] = temp; } } while (loSwap < hiSwap);

© Janice Regan, CMPT 102, Sept Quicksort function: 4 /* put pivot back in correct position */ InArray[loBound] = InArray[hiSwap]; InArray[hiSwap] = pivot; Quicksort(InArray, loBound, hiSwap-1); Quicksort(InArray, hiSwap+1, hiBound); }