Ch 5-2. Arrays Part 2

Size: px
Start display at page:

Download "Ch 5-2. Arrays Part 2"

Transcription

1 Ch 5-2. Arrays Part 2 March 29, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : ; Fax : ytkim@yu.ac.kr)

2 Outline Programming with Arrays Searching Binary search Bubble sorting Selection sorting Multidimensional Arrays ch

3 Search ( 탐색 ) Examples of Searching an Array search an array (e.g., array of student numbers for all students in a given course) for a given value (a particular student) Search Schemes Sequential search ( 순차탐색 ) Binary search ( 이진탐색 ) ch

4 Sequential Searching Searching an Array (1) ch

5 Searching an Array (2) ch

6 Searching an Array (3) ch

7 Searching an Array (4) ch

8 Binary Search with Recursion Binary Search Algorithm precondition: given data array, data[0..n-1], is sorted vkey = value key to be searched Procedure BinarySearch(int data[], int N, int vkey, int left, int right) //int data[0..n-1]; // sorted array //int vkey: value key to be searched // int left, int right: index of data[] range to be searched if (left > right) found = false; else { mid = approximate midpoint between left and right; // e.g.) mid = (left + right) / 2 if (vkey == data[mid]) { found = true; location = mid; } else if (vkey < data[mid]) BinarySearch(data, N, vkey, left, mid-1); else BinarySearch(data, N, vkey, mid+1, right); } ch

9 Example Execution of Binary Search Function data[n], N = 20; vkey = 9; left = 0 right = 19 mid = (0+19)/2 => 9 vkey < data[9] left = 0 right = 8 mid = (0+8)/2 => 4 vkey > data[4] left = 5 right = 8 mid = (5+8)/2 => 6 vkey < data[6] left = 5 right = 5 mid = (5+5)/2 => 5 vkey == data[5] found=true; location = 5; ch

10 Sorting ( 정렬 ) Sorting array A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: The output is in non-decreasing order (each element is no smaller than the previous element according to the desired total order); The output is a permutation (reordering) of the input. Further, the data is often taken to be in an array, which allows random access, rather than a list, which only allows sequential access, though often algorithms can be applied with suitable modification to either type of data. ch

11 Bubble Sorting Bubble Sorting Algorithm 1. Compare 1 st two elements and exchange them if they are out of order. 2. Move down one element and compare 2 nd and 3 rd elements. Exchange if necessary. Continue until the end of the array. 3. Pass through array again, repeating process and exchanging as necessary. 4. Repeat until a pass is made with no exchanges. Example of Bubble sort (increasing order) 1-st process 2-nd process 3-rd process ch

12 // This program uses the bubble sort algorithm // to sort an array in ascending order. #include <iostream> using namespace std; // Function prototypes void bubblesort(int [], int); void showarray(int [], int); int main() { const int SIZE = 6; int values[size] = {7, 2, 3, 8, 9, 1}; } cout << "The unsorted values are: n"; showarray(values, SIZE); bubblesort(values, SIZE); cout << "The sorted values are: n"; showarray(values, SIZE); return 0; ch

13 void bubblesort(int array[], int size) { int temp; bool swap; do { swap = false; for (int i = 0; i < (size - 1); i++) { if (array[i] > array[i + 1]) { temp = array[i]; // if out-of-order, then swap array[i] = array[i + 1]; array[i + 1] = temp; swap = true; } // end if } // end for } while (swap); } void showarray(int array[], int size) { for (int i = 0; i < size; i++) cout << array[i] << " "; cout << endl; } ch

14 Analysis of the Performance of Bubble Sorting Comparisons of each elements in each round multiple swapping of elements in each round ch

15 Selection Sorting Selection Sort Algorithm ch

16 Basic selection algorithm for array Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n 2 ) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited. The algorithm works as follows: Find the minimum value in the list Swap it with the value in the first position Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) ch

17 procedure selectionsort( a[] : array of sortable items, n: size ) 1. /* a[0] to a[n-1] is the array to sort */ 2. int ipos, jpos; 3. int imin; /* advance the position through the entire array */ 6. for (ipos = 0; ipos < n-1; ipos++) { // outer loop 7. /* find the min element in the unsorted a[ipos.. n-1] */ 8. imin = ipos; /* assume the min is the first element */ /* test against all other elements */ 11. for (jpos = ipos+1; jpos < n; jpos ++) { // inner loop 12. /* if this element is less, then it is the new minimum */ 13. if (a[jpos] < a[imin]) { 14. /* found new minimum; remember its index */ 15. imin = jpos; 16. } // end if 17. } // end of inner-loop /* imin is the index of the minimum element. 20. Swap it with the current position */ 21. if ( imin!= ipos ) { 22. swap(a, ipos, imin); 23. } 24. } // end of outer-loop 25. end procedure ch

18 Divide and Conquer Quick Sorting Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the lower elements and the higher elements. Quicksort can then recursively sort the sub-lists. Steps in Quick Sort Pick an element, called a pivot, from the list. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. ch

19 function quicksort('array') // Simple Version if length('array') 1 return 'array' // an array of zero or one elements is already sorted select and remove a pivot value 'pivot' from 'array' create empty lists 'less' and 'greater' for each 'x' in 'array' if 'x' 'pivot' then append 'x' to 'lesser' else append 'x' to 'greater' return concatenate(quicksort('lesser'), 'pivot', quicksort('greater')) // two recursive calls Problems of simple quicksort version need additional memory space for lesser and greater memory is limited valuable resource in most embedded systems => need in-place version ch

20 In-place version of QuickSorting // left is the index of the leftmost element of the array // right is the index of the rightmost element of the array // (inclusive) number of elements in subarray: right-left+1 int partition(array[], left, right, pivotindex) { pivotvalue = array[pivotindex]; swap array[pivotindex] and array[right] // Move pivot to right end storeindex = left; for (i = left; i<= right 1; i++) // left i< right { if (array[i] pivotvalue) { swap array[i] and array[storeindex]; storeindex = storeindex + 1; } } swap array[storeindex] and array[right]; // Move pivot to its final place return storeindex; Yeungnam } University (YU-ANTL) ch

21 function quicksort(array, left, right) { if (left >= right) return; else if (left < right) // subarray of 0 or 1 elements already sorted select a pi (pivotindex) in the range left pi right // e.g.) (left + right) / 2 newpi = partition(array, left, right, pi); // element at newpivotindex (newpi) is now at its final position if (left < (newpi-1)) quicksort(array, left, newpi - 1); // recursively sort elements on the left of pivotnewindex if ((newpi+1) < right) quicksort(array, newpi + 1, right); // recursively sort elements on the right of pivotnewindex } ch

22 Overall Process (1) QS(A[], 0, 12) pi = 0+(12-0)/2 = partition(a[], 0, 12, 6) newpi = QS(A[], 0, 4) pi = 0+(4-0)/2 = 2 partition(a[], 0, 4, 2) newpi = QS(A[], 2, 4) pi = 2+(4-2)/2 = 3 partition(a[], 2, 4, 3) newpi = ch

23 Overall Process (2) QS(A[], 6, 12) pi = 6+(12-6)/2 = partition(a[], 6, 12, 9) newpi = QS(A[], 8, 12) pi = 8+(12-8)/2= partition(a[], 8, 12, 10) newpi = QS(A[], 8, 9) pi = 8+(9-8)/2 = partition(a[], 8, 9, 8) newpi = ch

24 Overall Process (3) QS(A[], 11, 12) pi = 11+(12-11)/2 = partition(a[], 8, 9, 11) newpi = ch

25 Example execution of QuickSort ch

26 Comparisons of Sorting Algorithms Bubble sorting comparisons at each consecutive elements at each inner loop swap the consecutive elements, if necessary, at each inner loop Selection sorting comparisons to find the maximum or minimum element at each inner loop only 1 swap in each inner loop no more memory requirements Quick sorting divide and conquer comparisons and swapping at each stage the number of elements for each stage is divided, recursively over head in function call: more memory requirements ch

27 Multidimensional Arrays Arrays with more than one index char page[30][100]; Two indexes: An "array of arrays" Visualize as: page[0][0], page[0][1],, page[0][99] page[1][0], page[1][1],, page[1][99] page[29][0], page[29][1],, page[29][99] C++ allows any number of indexes Typically no more than two ch

28 Multidimensional Array Parameters Similar to one-dimensional array 1 st dimension size not given Provided as second parameter 2 nd dimension size IS given Example: void DisplayPage(const char p[][size], int sizedimension1) { for (int index1=0; index1<sizedimension1; index1++) { for (int index2=0; index2 < SIZE; index2++) cout << p[index1][index2]; cout << endl; } } ch

29 Display 5.10 Two-dimensional Array grade quiz 1 quiz 2 quiz 3 student 1 student 2 student 3 student 4 grade[0][0] grade[0][1] grade[0][2] grade[1][0] grade[1][1] grade[1][2] grade[2][0] grade[2][1] grade[2][2] grade[3][0] grade[3][1] grade[3][2] ch

30 Display 5.11 quiz 1 quiz 2 quiz 3 student 1 student 2 student 3 student stave[0] 1.0 stave[1] 7.7 stave[2] 7.3 stave[3] 7.0 quizave[0] 5.0 quizave[0] 7.5 quizave[0] ch

31 Display 5.9 Two-Dimensional Array //Reads quiz scores for each student into the two-dimensional array grade. //Computes the average score for each student and //the average score for each quiz. Displays the quiz scores and the averages. #include <iostream> #include <iomanip> using namespace std; const int NUMBER_STUDENTS = 4, NUMBER_QUIZZES = 3; void computestave(const int grade[][number_quizzes], double stave[]); //Precondition: Global constant NUMBER_STUDENTS and NUMBER_QUIZZES //are the dimensions of the array grade. Each of the indexed variables //grade[stnum-1, quiznum-1] contains the score for student stnum on quiz quiznum. //Postcondition: Each stave[stnum-1] contains the average for student number stunum. void computequizave(const int grade[][number_quizzes], double quizave[]); //Precondition: Global constant NUMBER_STUDENTS and NUMBER_QUIZZES //are the dimensions of the array grade. Each of the indexed variables //grade[stnum-1, quiznum-1] contains the score for student stnum on quiz quiznum. //Postcondition: Each quizave[quiznum-1] contains the average for quiz numbered //quiznum. void display(const int grade[][number_quizzes], const double stave[], const double quizave[]); //Precondition: Global constant NUMBER_STUDENTS and NUMBER_QUIZZES are the //dimensions of the array grade. Each of the indexed variables grade[stnum-1, //quiznum-1] contains the score for student stnum on quiz quiznum. Each //stave[stnum-1] contains the average for student stunum. Each quizave[quiznum-1] //contains the average for quiz numbered quiznum. //Postcondition: All the data in grade, stave, and quizave have been output. ch

32 Display 5.9 Two-Dimensional Array (2) int main( ) { int grade[number_students][number_quizzes]; double stave[number_students]; double quizave[number_quizzes]; grade[0][0] = 10; grade[0][1] = 10; grade[0][2] = 10; grade[1][0] = 2; grade[1][1] = 0; grade[1][2] = 1; grade[2][0] = 8; grade[2][1] = 6; grade[2][2] = 9; grade[3][0] = 8; grade[3][1] = 4; grade[3][2] = 10; } computestave(grade, stave); computequizave(grade, quizave); display(grade, stave, quizave); return 0; void computestave(const int grade[][number_quizzes], double stave[]) { for (int stnum = 1; stnum <= NUMBER_STUDENTS; stnum++) {//Process one stnum: double sum = 0; for (int quiznum = 1; quiznum <= NUMBER_QUIZZES; quiznum++) sum = sum + grade[stnum-1][quiznum-1]; //sum contains the sum of the quiz scores for student number stnum. stave[stnum-1] = sum/number_quizzes; //Average for student stnum is the value of stave[stnum-1] } } ch

33 Display 5.9 Two-Dimensional Array (3) void computequizave(const int grade[][number_quizzes], double quizave[]) { for (int quiznum = 1; quiznum <= NUMBER_QUIZZES; quiznum++) {//Process one quiz (for all students): double sum = 0; for (int stnum = 1; stnum <= NUMBER_STUDENTS; stnum++) sum = sum + grade[stnum-1][quiznum-1]; //sum contains the sum of all student scores on quiz number quiznum. quizave[quiznum-1] = sum/number_students; //Average for quiz quiznum is the value of quizave[quiznum-1] } } ch

34 Display 5.9 Two-Dimensional Array (4) void display(const int grade[][number_quizzes], const double stave[], const double quizave[]) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); cout << setw(10) << "Student" << setw(5) << "Ave" << setw(15) << "Quizzes n"; for (int stnum = 1; stnum <= NUMBER_STUDENTS; stnum++) {//Display for one stnum: cout << setw(10) << stnum << setw(5) << stave[stnum-1] << " "; for (int quiznum = 1; quiznum <= NUMBER_QUIZZES; quiznum++) cout << setw(5) << grade[stnum-1][quiznum-1]; cout << endl; } } cout << "Quiz averages = "; for (int quiznum = 1; quiznum <= NUMBER_QUIZZES; quiznum++) cout << setw(5) << quizave[quiznum-1]; cout << endl; ch

35 Homework Test driver program to measure the elapsed time of sorting functions. In order to test a function as a module in a large scale system, a driver program is temporarily prepared and used. This driver program prepares the testing conditions (i.e., arrays to be sorted, input data file and output data files, etc.), and invokes the module to be tested. The measurement of the elapsed time taken to process some function, is usually performed with system clock readings. In Visual C++, QueryPerformanceCounter (LARGE_INTEGER & time) provides the current time in microsecond. So, reading the system time before and after the function call can provide the time taken for the function call. Write a driver program should prepare 6 integer arrays with 10, 100, 1000, 10,000, 100,000 and 1,000,000 non-duplicated data elements using rand() function. Using the test driver program should check the time taken for the selection sorting and quick sorting algorithm for the 6 integer arrays, individually, and print out the elapsed times for each array size. For each case, print out the first 100 (or the total number of elements) and last 100 elements, before sorting and after sorting. ch

36 ch

37 5-2.2 Measurement of elapsed processing times for binary searching functions. Write a pseudo code for searching an array. Based on the pseudo code, write C++ program of binarysearch(int ma[], int size, int vkey). Write a test driver program that prepares eight integer arrays of 100, 500, 1,000, 5,000, 10,000, 50,000, 100,000, and 500,000 nonduplicated data elements using genbigrandarray(int ma[], int range) function. The generated array should be sorted by using selectionsort() function. The test driver randomly generates vkey, invokes the binarysearch() function, and measures the processing time of each sorting and searching function. Compare and explain the results of measurements of processing time of selectionsort() and quicksort() function with different data size. Explain the general formula for the time taken for selectionsort() and quicksort() from a non-sorted array of size N. ch

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim 2014-1 Ch 6-1. Structures March 30, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742

More information

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. Task Description. Selection Sort. Should we worry about speed? Sorting Should we worry about speed? Task Description We have an array of n values in any order We need to have the array sorted in ascending or descending order of values 2 Selection Sort Select the smallest

More information

Basic Programming Algorithms. 1-D Arrays

Basic Programming Algorithms. 1-D Arrays Basic Programming Algorithms 1-D Arrays 1-D fixed-size array Dim arr(5) As Double 0-base, 6 elements Dim arr(0 to 5) As Double 0-base, 6 elements Dim arr(1 to 5) As Double 1-base, 5 elements Option Base

More information

First Swedish Workshop on Multi-Core Computing MCC 2008 Ronneby: On Sorting and Load Balancing on Graphics Processors

First Swedish Workshop on Multi-Core Computing MCC 2008 Ronneby: On Sorting and Load Balancing on Graphics Processors First Swedish Workshop on Multi-Core Computing MCC 2008 Ronneby: On Sorting and Load Balancing on Graphics Processors Daniel Cederman and Philippas Tsigas Distributed Computing Systems Chalmers University

More information

Chapter 10 - Notes Applications of Arrays

Chapter 10 - Notes Applications of Arrays Chapter - Notes Applications of Arrays I. List Processing A. Definition: List - A set of values of the same data type. B. Lists and Arrays 1. A convenient way to store a list is in an array, probably a

More information

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE Looking for something COMP 10 EXPLORING COMPUTER SCIENCE Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE Searching algorithms Linear search Complexity Sorting algorithms

More information

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider:

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider: Efficiency COMP1511 focuses on writing programs. Effciency is also important. Often need to consider: execution time memory use. A correct but slow program can be useless. Efficiency often depends on the

More information

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 (8th ed) Gaddis: 8, 20.6,20.8 (9th ed) CS 5301 Fall 2018 Jill Seaman!1 Definitions of Search and Sort! Search: find a given item in a list, return the position

More information

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Spring 2015 Jill Seaman 1 Definitions of Search and Sort! Search: find a given item in a list, return the position of the item, or -1 if not found.!

More information

Arrays. Week 4. Assylbek Jumagaliyev

Arrays. Week 4. Assylbek Jumagaliyev Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays

More information

Ch 4. Parameters and Function Overloading

Ch 4. Parameters and Function Overloading 2014-1 Ch 4. Parameters and Function Overloading March 19, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

Ch 6. Structures and Classes

Ch 6. Structures and Classes 2013-2 Ch 6. Structures and Classes September 1, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Fall 2014 Jill Seaman 1 Definitions of Search and Sort! Search: find a given item in a list, return the position of the item, or -1 if not found.!

More information

CSCI 2170 Algorithm Analysis

CSCI 2170 Algorithm Analysis CSCI 2170 Algorithm Analysis Given two solutions (algorithms) for a given problem, which one is better? In computer science, we measure the quality of algorithms in terms of its memory and time efficiency.

More information

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return 0. How would we write the BinaryHeap siftdown function recursively? [0] 6 [1] [] 15 10 Name: template class BinaryHeap { private: int maxsize; int numitems; T * heap;... [3] [4] [5] [6] 114 0

More information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types Chapter 4 - Arrays 1 4.1 Introduction 4.2 Arrays 4.3 Declaring Arrays 4.4 Examples Using Arrays 4.5 Passing Arrays to Functions 4.6 Sorting Arrays 4.7 Case Study: Computing Mean, Median and Mode Using

More information

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements. Sorting Sorting is ordering a list of elements. Types of sorting: There are many types of algorithms exist based on the following criteria: Based on Complexity Based on Memory usage (Internal & External

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8

More information

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays The University Of Michigan Lecture 07 Andrew M. Morgan Sorting Arrays Element Order Of Arrays Arrays are called "random-access" data structures This is because any element can be accessed at any time Other

More information

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011 The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms Divide and Conquer Algorithms: Advanced Sorting Prichard Ch. 10.2: Advanced Sorting Algorithms 1 Sorting Algorithm n Organize a collection of data into either ascending or descending order. n Internal

More information

C++ PROGRAMMING SKILLS Part 4: Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median

More information

LECTURE 17. Array Searching and Sorting

LECTURE 17. Array Searching and Sorting LECTURE 17 Array Searching and Sorting ARRAY SEARCHING AND SORTING Today we ll be covering some of the more common ways for searching through an array to find an item, as well as some common ways to sort

More information

Merge- and Quick Sort

Merge- and Quick Sort Merge- and Quick Sort Merge Sort Quick Sort Exercises Unit 28 1 Merge Sort Merge Sort uses the algorithmic paradigm of divide and conquer: Divide the block into two subblocks of equal size; Merge Sort

More information

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

More on Arrays CS 16: Solving Problems with Computers I Lecture #13 More on Arrays CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #12 due today No homework assigned today!! Lab #7 is due on Monday,

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 0-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips Lab6 Array I Objectivities 1. Using rand to generate random numbers and using srand to seed the random-number generator. 2. Declaring, initializing and referencing arrays. 3. The follow-up questions and

More information

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Sorting o Simple: Selection Sort and Insertion Sort o Efficient: Quick Sort and Merge Sort Searching o Linear o Binary Reading for this lecture: http://introcs.cs.princeton.edu/python/42sort/

More information

Lecture Notes on Quicksort

Lecture Notes on Quicksort Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 February 5, 2015 1 Introduction In this lecture we consider two related algorithms for sorting that achieve

More information

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for Ch 8. earching and orting Arrays 8.1 and 8.3 only C 2308 pring 2013 Jill eaman efinitions of earch and ort! earch: find an item in an array, return the index to the item, or -1 if not found.! ort: rearrange

More information

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function. Recursion! A Recursive function is a functions that calls itself.! Recursive functions can be useful in solving problems that can be broken down into smaller or simpler subproblems of the same type.! A

More information

Sorting. Order in the court! sorting 1

Sorting. Order in the court! sorting 1 Sorting Order in the court! sorting 1 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of the primary reasons why people use computers in the first place

More information

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative order of elements with equal keys 3) In-place: only

More information

Lecture Notes on Quicksort

Lecture Notes on Quicksort Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 September 20, 2012 1 Introduction In this lecture we first sketch two related algorithms for sorting that

More information

Function Call Example

Function Call Example Function Call Example A Function Call Example (1) ch 3-25 A Function Call Example (2) ch 3-26 Alternative Function Declaration Recall: Function declaration is "information for compiler Compiler only needs

More information

Ch 8. Operator Overloading, Friends, and References

Ch 8. Operator Overloading, Friends, and References 2014-1 Ch 8. Operator Overloading, Friends, and References May 28, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel

More information

Ch 1. Algorithms and Basic C++ Programming

Ch 1. Algorithms and Basic C++ Programming 2013-2 Ch 1. Algorithms and Basic C++ Programming July 1, 2013 Dept. of Information & Communication Engineering College of Engineering Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742

More information

Sorting. Bringing Order to the World

Sorting. Bringing Order to the World Lecture 10 Sorting Bringing Order to the World Lecture Outline Iterative sorting algorithms (comparison based) Selection Sort Bubble Sort Insertion Sort Recursive sorting algorithms (comparison based)

More information

Sorting Algorithms. + Analysis of the Sorting Algorithms

Sorting Algorithms. + Analysis of the Sorting Algorithms Sorting Algorithms + Analysis of the Sorting Algorithms Insertion Sort What if first k elements of array are already sorted? 4, 7, 12, 5, 19, 16 We can shift the tail of the sorted elements list down and

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

Sorting. Order in the court! sorting 1

Sorting. Order in the court! sorting 1 Sorting Order in the court! sorting 1 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of the primary reasons why people use computers in the first place

More information

UNIT 7. SEARCH, SORT AND MERGE

UNIT 7. SEARCH, SORT AND MERGE UNIT 7. SEARCH, SORT AND MERGE ALGORITHMS Year 2017-2018 Industrial Technology Engineering Paula de Toledo CONTENTS 7.1. SEARCH 7.2. SORT 7.3. MERGE 2 SEARCH Search, sort and merge algorithms Search (search

More information

Divide and Conquer Algorithms: Advanced Sorting

Divide and Conquer Algorithms: Advanced Sorting Divide and Conquer Algorithms: Advanced Sorting (revisit) Properties of Growth-rate functions(1/3) 1. You can ignore low-order terms in an algorithm's growth-rate function. O(n 3 +4n 2 +3n) it is also

More information

To become familiar with array manipulation, searching, and sorting.

To become familiar with array manipulation, searching, and sorting. ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,

More information

Analyzing Complexity of Lists

Analyzing Complexity of Lists Analyzing Complexity of Lists Operation Sorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) O( n ) Insert( L, x ) O(logn) O( n ) + O( 1 ) O( 1 ) + O(

More information

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

C/C++ Programming Lecture 18 Name:

C/C++ Programming Lecture 18 Name: . The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search

More information

Ch 7. Constructors and Other Tools

Ch 7. Constructors and Other Tools 2013-2 Ch 7. Constructors and Other Tools September 3, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Sorting Quicksort analysis Bubble sort November 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Quicksort analysis How long does Quicksort take to run? Let's consider the best and the worst case These differ

More information

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 7 Quicksort 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than

More information

Linear Search. Sorting Algorithms. linear search code. Sorting in Ascending Order Selection Sort. Selection sort algorithm

Linear Search. Sorting Algorithms. linear search code. Sorting in Ascending Order Selection Sort. Selection sort algorithm Linear Search Additional CPSC1620 topics Searching, Sorting, Big(O) template functions, classes The idea of a linear search is to walk through the entire list until the value is found. The list does not

More information

Programming challenges in C++

Programming challenges in C++ Programming challenges in C++ 7. Using functions and improving sorting algorithms Nacho Iborra IES San Vicente This work is licensed under the Creative Commons Attribution- NonCommercial-ShareAlike 4.0

More information

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays Chapter 5 Arrays Learning Objectives Introduction to Arrays Declaring and referencing arrays For-loops and arrays Arrays in memory Arrays in Functions Arrays as function arguments, return values Programming

More information

Cpt S 122 Data Structures. Sorting

Cpt S 122 Data Structures. Sorting Cpt S 122 Data Structures Sorting Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Sorting Process of re-arranging data in ascending or descending order Given

More information

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms Sorting Savitch Chapter. Why sort Easier to search (binary search) Sorting used as a step in many algorithms Sorting algorithms There are many algorithms for sorting: Selection sort Insertion sort Bubble

More information

QuickSort. CIS 15 : Spring 2007

QuickSort. CIS 15 : Spring 2007 QuickSort CIS 15 : Spring 2007 Functionalia TEA! HW 1 is DUE FRIDAY 23rd, 11:59 PM Do the BASIC Program First! Turn in Basic Program and Challenges Seperately Today: Binary Search Example QuickSort Submitting

More information

Ch 6-2. File Input / Output

Ch 6-2. File Input / Output 2014-1 Ch 6-2. File Input / Output March 30, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax

More information

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries 2013-2 DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 22, 2013 Advanced

More information

Sorting is ordering a list of objects. Here are some sorting algorithms

Sorting is ordering a list of objects. Here are some sorting algorithms Sorting Sorting is ordering a list of objects. Here are some sorting algorithms Bubble sort Insertion sort Selection sort Mergesort Question: What is the lower bound for all sorting algorithms? Algorithms

More information

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order. Sorting The sorting problem is defined as follows: Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS Fast sorting algorithms Shellsort, Mergesort, Quicksort Summary of the previous lecture Why sorting is needed? Examples from everyday life What are the basic operations in

More information

CMPS 221 Sample Final

CMPS 221 Sample Final Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,

More information

PIC 10A. Final Review: Part I

PIC 10A. Final Review: Part I PIC 10A Final Review: Part I Final exam The final exam is worth 30% of your grade, same weight as 2 midterms. Could be 50% if grading option 2 turns out better for you. Length is also roughly 2 midterms

More information

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order S O R T I N G Sorting is interpreted as arranging data in some particular order. In this handout we discuss different sorting techniques for a list of elements implemented as an array. In all algorithms

More information

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std; r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int

More information

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1 Sonoma State University Computer Science Department CS 115 Fall 2010 Watts Project 3 Part 1 For this part of the project you will be modifying one of the programs you submitted for Lab 8 to calculate your

More information

Search,Sort,Recursion

Search,Sort,Recursion Search,Sort,Recursion Searching, Sorting and Recursion Searching Linear Search Inserting into an Array Deleting from an Array Selection Sort Bubble Sort Binary Search Recursive Binary Search Searching

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Sort: Divide & Conquer. Data Structures and Algorithms Emory University Jinho D. Choi

Sort: Divide & Conquer. Data Structures and Algorithms Emory University Jinho D. Choi Sort: Divide & Conquer Data Structures and Algorithms Emory University Jinho D. Choi Comparison-Based Sort Comparison complexities Selection-based Insertion-based Selection Heap Insertion Shell (Knuth)

More information

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14 Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14 Ziad Matni Dept. of Computer Science, UCSB Administra:ve Turn in Homework #12 Homework #13 is due Tuesday Lab

More information

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2 MPATE-GE 2618: C Programming for Music Technology Unit 4.2 Quiz 1 results (out of 25) Mean: 19.9, (standard deviation = 3.9) Equivalent to 79.1% (SD = 15.6) Median: 21.5 High score: 24 Low score: 13 Pointer

More information

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: Quicksort Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

More information

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 13 Recursion Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Recursive void Functions Tracing recursive calls Infinite recursion, overflows Recursive Functions that Return

More information

Sorting is a problem for which we can prove a non-trivial lower bound.

Sorting is a problem for which we can prove a non-trivial lower bound. Sorting The sorting problem is defined as follows: Sorting: Given a list a with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

More information

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3) Outline Part 5. Computational Complexity (2) Complexity of Algorithms Efficiency of Searching Algorithms Sorting Algorithms and Their Efficiencies CS 200 Algorithms and Data Structures 1 2 (revisit) Properties

More information

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort Principles of Imperative Computation V. Adamchik CS 15-1 Lecture Carnegie Mellon University Sorting Sorting Sorting is ordering a list of objects. comparison non-comparison Hoare Knuth Bubble (Shell, Gnome)

More information

Programming in OOP/C++

Programming in OOP/C++ Introduction Lecture 3-2 Programming in OOP/C++ Arrays Part (2) By Assistant Professor Dr. Ali Kattan 1 Arrays Examples Solutions for previous assignments Write a program to enter and store your name and

More information

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms C. L. Wyatt Today we will continue looking at sorting algorithms Bubble sort Insertion sort Merge sort Quick sort Common Sorting Algorithms

More information

Computer Science Foundation Exam

Computer Science Foundation Exam Computer Science Foundation Exam December 18, 015 Section I B COMPUTER SCIENCE NO books, notes, or calculators may be used, and you must work entirely on your own. SOLUTION Question # Max Pts Category

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same

More information

Problem Solving: Storyboards for User Interaction

Problem Solving: Storyboards for User Interaction Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Recursive Algorithms CS 180 Sunil Prabhakar Department of Computer Science Purdue University Recursive Algorithms Within a given method, we are allowed to call other accessible methods. It is also possible

More information

Unit-2 Divide and conquer 2016

Unit-2 Divide and conquer 2016 2 Divide and conquer Overview, Structure of divide-and-conquer algorithms, binary search, quick sort, Strassen multiplication. 13% 05 Divide-and- conquer The Divide and Conquer Paradigm, is a method of

More information

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO? 8/5/10 TODAY'S OUTLINE Recursion COMP 10 EXPLORING COMPUTER SCIENCE Revisit search and sorting using recursion Binary search Merge sort Lecture 8 Recursion WHAT DOES THIS CODE DO? A function is recursive

More information

Arrays and Applications

Arrays and Applications Arrays and Applications 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2014 Instructor: Dr. Asish Mukhopadhyay What s an array Let a 0, a 1,, a n-1 be a sequence

More information

Topics. Sorting. Sorting. 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort

Topics. Sorting. Sorting. 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort Topics 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort 2) How can we search for an element in an array? a) Linear Search b) Binary Search Slides #15 Sections 9.1-9.5 Sorting and

More information

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych CS125 : Introduction to Computer Science Lecture Notes #38 and #39 Quicksort c 2005, 2003, 2002, 2000 Jason Zych 1 Lectures 38 and 39 : Quicksort Quicksort is the best sorting algorithm known which is

More information

Bubble sort starts with very first two elements, comparing them to check which one is greater.

Bubble sort starts with very first two elements, comparing them to check which one is greater. Bubble Sorting: Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they

More information

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with

More information

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8.

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Fa 2017 Ji Seaman 1 Definitions of Search and Sort Search: find a given item in a ist, return the position of the item, or -1 if not found. Sort:

More information

Chapter Four: Loops II

Chapter Four: Loops II Chapter Four: Loops II Slides by Evan Gallagher & Nikolay Kirov Chapter Goals To understand nested loops To implement programs that read and process data sets To use a computer for simulations Processing

More information

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Thursday, 5/17 in this classroom Starts at 2:00 PM **SHARP** Please

More information

Sorting and Selection

Sorting and Selection Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element

More information

Overview of Sorting Algorithms

Overview of Sorting Algorithms Unit 7 Sorting s Simple Sorting algorithms Quicksort Improving Quicksort Overview of Sorting s Given a collection of items we want to arrange them in an increasing or decreasing order. You probably have

More information

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No. The American University in Cairo Computer Science & Engineering Department CSCE 106 Instructor: Final Exam Fall 2010 Last Name :... ID:... First Name:... Section No.: EXAMINATION INSTRUCTIONS * Do not

More information

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable EXERCISE ANSWERS Chapter 7 Exam Preparation Exercises 1 Function call The mechanism that transfers control to the body of a function Argument list A mechanism by which functions communicate with each other;

More information