Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%

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.
Understanding the Need for Sorting Records
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 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
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
CSE 373: Data Structures and Algorithms
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
C++ for Engineers and Scientists Third Edition
Searching and Sorting Arrays
Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
Chapter 16: Searching, Sorting, and the vector Type.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
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:
February 4, 2005 Searching and Sorting Arrays. Searching.
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
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.
CSCI 51 Introduction to Programming March 12, 2009.
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 and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
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.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Searching Arrays Linear search Binary search small arrays
Chapter 9: Sorting and Searching Arrays
Introduction to Search 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.
Introduction to Programming
CISC181 Introduction to Computer Science Dr
Chapter 8 – Searching and Sorting Arrays
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Sorting and Complexity
Presentation transcript:

Programming Sorting Arrays

COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50% of all computing power is used for sorting activities. l Possible reasons: n Many applications require sorting; n Many applications perform sorting when they don't have to; n Many applications use inefficient sorting algorithms.

COMP102 Prog. Fundamentals. Sorting I/ Slide 3 Sorting Applications l To prepare a list of student ID, names, and scores in a table (sorted by ID or name) for easy checking. l To prepare a list of scores before letter grade assignment. l To produce a list of horses after a race (sorted by the finishing times) for payoff calculation. l To prepare an originally unsorted array for ordered binary searching.

COMP102 Prog. Fundamentals. Sorting I/ Slide 4 Some Sorting Methods l Selection sort l Bubble sort l Shell sort (a simple but faster sorting method than above; see p.331 of Numerical Recipes in C, 2nd ed., by William H. Press et al, Cambridge University Press, 1992) l Quick sort (a very efficient sorting method for most applications; p , ibid.)

COMP102 Prog. Fundamentals. Sorting I/ Slide 5 Ex. 1A: Selection Sort l Selection sort performs sorting by repeatedly putting the largest element in the unsorted portion of the array to the end of this unsorted portion until the whole array is sorted. l It is similar to the way that many people do their sorting.

COMP102 Prog. Fundamentals. Sorting I/ Slide 6 Ex. 1A: Selection Sort l Algorithm 1. Define the entire array as the unsorted portion of the array 2. While the unsorted portion of the array has more than one element:  Find its largest element.  Swap with last element (assuming their values are different).  Reduce the size of the unsorted portion of the array by 1.

Before sorting After pass After pass After pass After pass

// Sort array of integers in ascending order void select(int data[], // in/output: array int size){ // input: array size int temp; // for swap int max_index; // index of max value for (int rightmost=size-1; rightmost>0; rightmost--){ //find the largest item in the unsorted portion //rightmost is the end point of the unsorted part of array max_index = 0; //points the largest element for ( int current=1; current<=rightmost; current++) if (data[current] > data[max_index]) max_index = current; //swap the largest item with last item if necessary if (data[max_index] > data[rightmost]){ temp = data[max_index]; // swap data[max_index] = data[rightmost]; data[rightmost] = temp; } }}

const int array_size = 8; int main() { int list[array_size] = {14, 2, 10, 5, 1, 3, 17, 7}; int index, ans; cout << "Before sorting: "; for (index = 0; index<array_size; index++) cout << list[index] <<" "; cout << endl; cout << "Enter sorting method 1(select), 2(bubble):"; cin >> ans; if (ans == 1) select(list, array_size); else bubble(list, array_size); cout << endl << "After sorting: "; for (index = 0; index<array_size; index++) cout << list[index] <<" "; cout << endl; return 0; }

COMP102 Prog. Fundamentals. Sorting I/ Slide 10 Ex. 1B: Bubble Sort l Bubble sort examines the array from start to finish, comparing elements as it goes. l Any time it finds a larger element before a smaller element, it swaps the two. l In this way, the larger elements are passed towards the end. l The largest element of the array therefore "bubbles" to the end of the array. l Then it repeats the process for the unsorted portion of the array until the whole array is sorted.

COMP102 Prog. Fundamentals. Sorting I/ Slide 11 Ex. 1A: Bubble Sort l Bubble sort works on the same general principle as shaking a soft drink bottle. l Right after shaking, the contents are a mixture of bubbles and soft drink, distributed randomly. l Because bubbles are lighter than the soft drink, they rise to the surface, displacing the soft drink downwards. l This is how bubble sort got its name, because the smaller elements "float" to the top, while the larger elements "sink" to the bottom.

COMP102 Prog. Fundamentals. Sorting I/ Slide 12 Ex. 1B: Bubble Sort l Algorithm n Define the entire array as the unsorted portion of the array. n While the unsorted portion of the array has more than one element: 1. For every element in the unsorted portion, swap with the next neighbor if it is larger than the neighbor. 2. Reduce the size of the unsorted portion of the array by 1.

Before sorting outer=7, inner= outer=7, inner= outer=7, inner= outer=7, inner= outer=7, inner= outer=7, inner= outer=6, inner= outer=6, inner= outer=6, inner= outer=6, inner= outer=5, inner= outer=5, inner= outer=5, inner= outer=4, inner= outer=1, inner=

//Example1b: Bobble sort // Sort an array of integers in ascending order void bubble(int data[], // in/output: array int size){ // input: array size int temp; // for swap for(int outer=size-1; outer > 0; outer--){ for (int inner=0; inner < outer; inner++) { // traverse the nested loops if ( data[inner] > data[inner+1] ) { // swap current element with next // if the current element is greater temp = data[inner]; data[inner] = data[inner+1]; data[inner+1] = temp; } } // inner for loop } // outer for loop }

const int array_size = 12, string_size = 11; int main() { char month[array_size][string_size] = {"January", "February","March","April","May","June","July","August", "September", "October", "November", "December"}; int index, ans; cout << "Enter sorting method 1(select), 2(bubble): "; cin >> ans; if (ans == 1) select(month, array_size); else bubble(month, array_size); cout << endl << "After sorting: "; for (index = 0; index<array_size; index++) cout << month[index] <<" "; cout << endl; return 0; }

month[0]January\0 month[1]February\0 month[2]March\0 month[3]April\0 month[4]May\0 month[5]June\0 month[6]July\0 month[7]August\0 month[8]September\0 month[9]Octcber\0 month[10]November\0 month[11]December\0

// Sort array of strings in ascending order void select(char data[][string_size], // in/output: array int size){ // input: array size char temp[string_size]; // for swap int max_index; // index of max value for (int rightmost=size-1; rightmost>0; rightmost--){ // find the largest item max_index = 0; for(int current=1; current<=rightmost; current++) if (strcmp(data[current], data[max_index]) >0) max_index = current; // swap with last item if necessary if(strcmp(data[max_index], data[rightmost])>0){ strcpy(temp,data[max_index]); // swap strcpy(data[max_index],data[rightmost]); strcpy(data[rightmost], temp); for (int index=0; index< size; index++) cout << data[index] << " "; cout<<endl; }

// Sort an array of strings in ascending order void bubble(char data[][string_size], // in/output:array int size){ // input: array size char temp[string_size]; // for swap for(int outer=size-1 ; outer>0; outer--){ for (int inner=0; inner < outer; inner++) { // traverse the nested loops if ( strcmp(data[inner], data[inner+1])>0 ) { // swap current element with next // if the current element is greater strcpy(temp, data[inner]); strcpy(data[inner], data[inner+1]); strcpy(data[inner+1], temp); for (int index=0; index< size; index++) cout << data[index] << " "; cout<<endl; } } // inner for loop } // outer for loop }