 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
 2003 Prentice Hall, Inc. All rights reserved. Using Arrays in Abstract Data Types.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Searching Arrays Linear search Binary search small arrays
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Esempi Puntatori e Stringhe1 // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
 2003 Prentice Hall, Inc. All rights reserved. Using Arrays in Abstract Data Types.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Examples Using Arrays Passing arrays to functions Class GradeBook: store student grades.
 2003 Prentice Hall, Inc. All rights reserved. Using Arrays in Abstract Data Types.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Arrays as Lists with examples. 2 Review Arrays –Structures of related data items of the same type –Data items (called array elements) are stored at.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig04_03.cpp (1 of 2) 1 // Fig. 4.3: fig04_03.cpp 2 // Initializing an array. 3 #include 4 5.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Searching Arrays Linear search Binary search small arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
C++ Programming Lecture 15 Arrays – Part II
C++ Programming Lecture 15 Arrays – Part II
Using Arrays in Abstract Data Types
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays as Lists with examples.
CISC181 Introduction to Computer Science Dr
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Recursive Algorithms 1 Building a Ruler: drawRuler()
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort some data Massive amounts must be sorted Bubble sort (sinking sort) –Several passes through the array –Successive pairs of elements are compared If increasing order (or identical), no change If decreasing order, elements exchanged –Repeat these steps for every element –Time complexity: O(n 2 )

 2003 Prentice Hall, Inc. All rights reserved. 2 Sorting Arrays Example: –Go left to right, and exchange elements as necessary One pass for each element –Original: –Pass 1: (elements exchanged) –Pass 2: –Pass 3: (no changes needed) –Pass 4: –Pass 5: –Small elements "bubble" to the top (like 2 in this example)

 2003 Prentice Hall, Inc. All rights reserved. 3 Sorting Arrays Swapping variables int x = 3, y = 4; y = x; x = y; What happened? –Both x and y are 3! –Need a temporary variable Solution int x = 3, y = 4, temp = 0; temp = x; // temp gets 3 x = y; // x gets 4 y = temp; // y gets 3

 2003 Prentice Hall, Inc. All rights reserved. Outline // This program sorts an array's values into ascending order. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; int main() 13 { 14 const int arraySize = 10; // size of array a 15 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 16 int hold; // temporary location used to swap array elements cout << "Data items in original order\n"; // output original array 21 for ( int i = 0; i < arraySize; i++ ) 22 cout << setw( 4 ) << a[ i ]; 23

 2003 Prentice Hall, Inc. All rights reserved. Outline 5 24 // bubble sort 25 // loop to control number of passes 26 for ( int pass = 0; pass < arraySize - 1; pass++ ) // loop to control number of comparisons per pass 29 for ( int j = 0; j < arraySize - 1; j++ ) // compare side-by-side elements and swap them if 32 // first element is greater than second element 33 if ( a[ j ] > a[ j + 1 ] ) { 34 hold = a[ j ]; 35 a[ j ] = a[ j + 1 ]; 36 a[ j + 1 ] = hold; } // end if 39 Do a pass for each element in the array. If the element on the left (index j ) is larger than the element on the right (index j + 1 ), then we swap them. Remember the need of a temp variable.

 2003 Prentice Hall, Inc. All rights reserved. Outline 6 40 cout << "\nData items in ascending order\n"; // output sorted array 43 for ( int k = 0; k < arraySize; k++ ) 44 cout << setw( 4 ) << a[ k ]; cout << endl; return 0; // indicates successful termination } // end main Data items in original order Data items in ascending order

 2003 Prentice Hall, Inc. All rights reserved. 7 Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search –Compare each element of array with key value Start at one end, go to other –Useful for small and unsorted arrays Inefficient If search key not present, examines every element –Time complexity: O(n)

 2003 Prentice Hall, Inc. All rights reserved. 8 Searching Arrays: Linear Search and Binary Search Binary search –Only used with sorted arrays –Compare middle element with key If equal, match found If key < middle –Repeat search on first half of array If key > middle –Repeat search on last half –Faster than linear search “divide and conquer” Time complexity: O(log 2 n)

 2003 Prentice Hall, Inc. All rights reserved. Outline 9 1 // Fig. 4.19: fig04_19.cpp 2 // Linear search of an array. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int linearSearch( const int [], int, int ); // prototype int main() 12 { 13 const int arraySize = 100; // size of array a 14 int a[ arraySize ]; // create array a 15 int searchKey; // value to locate in a for ( int i = 0; i < arraySize; i++ ) // create some data 18 a[ i ] = 2 * i; cout << "Enter integer search key: "; 21 cin >> searchKey; // attempt to locate searchKey in array a 24 int element = linearSearch( a, searchKey, arraySize ); 25 Takes array, search key, and array size.

 2003 Prentice Hall, Inc. All rights reserved. Outline // display results 27 if ( element != -1 ) 28 cout << "Found value in element " << element << endl; 29 else 30 cout << "Value not found" << endl; return 0; // indicates successful termination } // end main // compare key to every element of array until location is 37 // found or until end of array is reached; return subscript of 38 // element if key or -1 if key not found 39 int linearSearch( const int array[], int key, int sizeOfArray ) 40 { 41 for ( int j = 0; j < sizeOfArray; j++ ) if ( array[ j ] == key ) // if found, 44 return j; // return location of key return -1; // key not found } // end function linearSearch

 2003 Prentice Hall, Inc. All rights reserved. Outline 11 Enter integer search key: 36 Found value in element 18 Enter integer search key: 37 Value not found

 2003 Prentice Hall, Inc. All rights reserved. Outline // Binary search of an array. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include using std::setw; // function prototypes 14 int binarySearch( const int [], int, int, int, int ); 15 void printHeader( int ); 16 void printRow( const int [], int, int, int, int ); int main() 19 { 20 const int arraySize = 15; // size of array a 21 int a[ arraySize ]; // create array a 22 int key; // value to locate in a for ( int i = 0; i < arraySize; i++ ) // create some data 25 a[ i ] = 2 * i; 26

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig04_20.cpp (2 of 6) 27 cout << "Enter a number between 0 and 28: "; 28 cin >> key; printHeader( arraySize ); // search for key in array a 33 int result = 34 binarySearch( a, key, 0, arraySize - 1, arraySize ); // display results 37 if ( result != -1 ) 38 cout << '\n' << key << " found in array element " 39 << result << endl; 40 else 41 cout << '\n' << key << " not found" << endl; return 0; // indicates successful termination } // end main 46

 2003 Prentice Hall, Inc. All rights reserved. Outline // function to perform binary search of an array 48 int binarySearch( const int b[], int searchKey, int low, 49 int high, int size ) 50 { 51 int middle; // loop until low subscript is greater than high subscript 54 while ( low <= high ) { // determine middle element of subarray being searched 57 middle = ( low + high ) / 2; // display subarray used in this loop iteration 60 printRow( b, low, middle, high, size ); 61 Determine middle element

 2003 Prentice Hall, Inc. All rights reserved. Outline // if searchKey matches middle element, return middle 63 if ( searchKey == b[ middle ] ) // match 64 return middle; else // if searchKey less than middle element, 69 // set new high element 70 if ( searchKey < b[ middle ] ) 71 high = middle - 1; // search low end of array // if searchKey greater than middle element, 74 // set new low element 75 else 76 low = middle + 1; // search high end of array 77 } return -1; // searchKey not found } // end function binarySearch Use the rule of binary search: If key equals middle, match If less, search low end If greater, search high end Loop sets low, middle and high dynamically. If searching the high end, the new low is the element above the middle.

 2003 Prentice Hall, Inc. All rights reserved. Outline // print header for output 84 void printHeader( int size ) 85 { 86 cout << "\nSubscripts:\n"; // output column heads 89 for ( int j = 0; j < size; j++ ) 90 cout << setw( 3 ) << j << ' '; cout << '\n'; // start new line of output // output line of - characters 95 for ( int k = 1; k <= 4 * size; k++ ) 96 cout << '-'; cout << endl; // start new line of output } // end function printHeader 101

 2003 Prentice Hall, Inc. All rights reserved. Outline // print one row of output showing the current 103 // part of the array being processed 104 void printRow( const int b[], int low, int mid, 105 int high, int size ) 106 { 107 // loop through entire array 108 for ( int m = 0; m < size; m++ ) // display spaces if outside current subarray range 111 if ( m high ) 112 cout << " "; // display middle element marked with a * 115 else if ( m == mid ) // mark middle value 118 cout << setw( 3 ) << b[ m ] << '*'; // display other elements in subarray 121 else 122 cout << setw( 3 ) << b[ m ] << ' '; cout << endl; // start new line of output } // end function printRow

 2003 Prentice Hall, Inc. All rights reserved. Outline 18 Enter a number between 0 and 28: 6 Subscripts: * * found in array element 3 Enter a number between 0 and 28: 25 Subscripts: * * * 28 24* 25 not found

 2003 Prentice Hall, Inc. All rights reserved. Outline 19 Enter a number between 0 and 28: 8 Subscripts: * * * 12 8* 8 found in array element 4