Sorting Arrays Chapter 10

Slides:



Advertisements
Similar presentations
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.
Advertisements

One Dimensional Arrays
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?
1 Sorting Animation Chapter Selection Sort Another way of sorting is the selection sort The main idea is to keep finding the smallest (and next.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Computer Programming Sorting and Sorting Algorithms 1.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
C++ for Engineers and Scientists Third Edition
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.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Arrays Multi-dimensional initialize & display Sorting Part II.
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.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
Shell Sort. Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort.
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.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays Chapter 7.
Sort Algorithm.
Sorting Dr. Yingwu Zhu.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Chapter 8 Multidimensional Arrays
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Introduction to Search Algorithms
Arrays 2.
Multi-dimensional Array
C++ Arrays.
CS1100 Computational Engineering
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
ITEC 2620M Introduction to Data Structures
Chapter 8 Arrays Objectives
Lecture 12 Oct 16, 02.
Review of Arrays and Pointers
Searching and Sorting 1-D Arrays
Sorting Dr. Yingwu Zhu.
Data Structures (CS212D) Week # 2: Arrays.
Multidimensional Arrays
Review of Everything Arrays
Multidimensional array
CS 1430: Programming in C++.
Arrays of Two-Dimensions
CHAPTER 2 Arrays and Vectors.
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Chapter 8 Arrays Objectives
CHAPTER 2 Arrays and Vectors.
Chapter 8 Multidimensional Arrays
Sorting Dr. Yingwu Zhu.
Insertion Sort and Shell Sort
Arrays Imran Rashid CTO at ManiWeber Technologies.
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
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.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

Sorting Arrays Chapter 10

Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays

Review Arrays You know how to declare, initialize, process arrays with loops, and pass them to functions: float stuff[10]={3, 4, 6, 8, 2, 1, 0}; for (int k=0; k<9; k++) stuff[k]=stuff[k+1]; Display(stuff, 10);

You can also pass one (or more) individual cells of an array to a function: int scores[8]={33, 54, 65, 84, 42, 61, 100, 53}; swap(scores[4], scores[1]); swap(scores[2], scores[7]); Notice a Pattern? void swap(int& x, int& y) { // exchanges the values of x , y: float temp = x; x = y; y = temp; }

Agenda Review of Arrays Sorting Arrays  Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays

Sorting Arrays Computer scientists often need to sort arrays –Why? Because it’s easier to find things in the array when it is sorted Most data looks better displayed in sorted form (phone books, employee records, Lacrosse games) How can we sort an array? What is the algorithm? A: There are several!!

Bubble Sort Bubble sort is one of the simplest sorting algorithms It proceeds through a sequence of iterations, each time moving the next largest item into its correct position On each iteration, it compares each pair of consecutive elements, moving the larger element up

Bubble Sort 55 22 99 66 55

Bubble Sort 55 22 99 66 > 55 ?

Bubble Sort 55 22 99 66 swap

Bubble Sort 22 55 99 66 55

Bubble Sort 22 55 99 66 > 55 ?

Bubble Sort 22 55 99 66 99

Bubble Sort 22 55 99 66 > 99 ?

Bubble Sort 22 55 99 66 swap

Bubble Sort 22 55 66 99 Notice how the data “bubbles up” through the array moving slowly, one bin at a time After N-1 “Passes” or “Sweeps”, the final array is guaranteed to be sorted in ascending order, no matter what input data

Bubble Sort #include <iostream.h> void print(float a[], int n); //Prints array a void sort(float a[], int n);//Sorts array a void swap(float& , float&);//Swaps a[j] and a[j+1] void main() { float a[] = {55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7}; print(a,8); sort(a,8); } void print(float a[], int n) for (int i=0; i<n-1; i++) cout<< a[i] << ", "; cout << a[n-1] << endl;

Bubble Sort (contd) void sort(float a[], int n) { for (int i=1; i<n; i++) for ( int j=0; j<n-1; j++) if(a[j] > a[j+1]) swap(a[j],a[j+1]); } void swap(float& x, float& y) float temp; temp=y; y=x; x=temp;

Selection Sort Another way of sorting is the selection sort The main idea is to keep finding the smallest (and next smallest) items in the array And move them into correct position (swap)

Selection Sort 1 2 3 55 22 99 66 data smallest small_pos 55 k 1 2 3 data 55 22 99 66 smallest small_pos 55 k 55 < smallest? F

Selection Sort 1 2 3 55 22 99 66 data smallest small_pos 55 k 1 2 3 data 55 22 99 66 smallest small_pos 55 k 22 < smallest? T

Selection Sort 1 2 3 55 22 99 66 data smallest small_pos 22 1 k 1 2 3 data 55 22 99 66 smallest small_pos 22 1 k 22 < smallest? T

Selection Sort 1 2 3 55 22 99 66 data smallest small_pos 22 1 k 1 2 3 data 55 22 99 66 smallest small_pos 22 1 k 99 < smallest? F

Selection Sort 1 2 3 55 22 99 66 data smallest small_pos 22 1 k 1 2 3 data 55 22 99 66 smallest small_pos 22 1 k 66 < smallest? F

Selection Sort—SWAP 1 2 3 55 22 99 66 Swap(data[k], data[small_pos]); 1 2 3 data 55 22 99 66 smallest small_pos 22 1 k Swap(data[k], data[small_pos]);

Selection Sort—Repeat 1 2 3 22 55 99 66 smallest small_pos 55 1 k 1 55 < smallest ? F

Selection Sort—Finding Smallest After (SIZE-1) iterations of the above, array is sorted The heart of this algorithm is finding the smallest element of the array (and it’s position or index small_pos): smallest=data[0]; // assume 0th cell small_pos=0; // is smallest for (n=0; n<SIZE; n++) // go thru array if (data[n]<smallest) // if smaller { small_pos=n; //save position smallest=data[n]; // and value }

Selection Sort—the whole function void Sort(int data[], int size) { int n, k, small_pos, smallest; for (k=0; k<size-1; k++) { smallest=data[k]; // assume kth cell small_pos=k; // is smallest for (n=k; n<SIZE; n++) if (data[n]<smallest)// if smaller { small_pos=n; //save position smallest=data[n]; // and value } Swap(data[k], data[small_pos]);

Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays 

Multidimensional Arrays The arrays we have looked at till now have been one-dimensional They are linear (or sequential) An array of arrays is called a multidimensional array A one-dimensional array of one-dimensional arrays is called a two-dimensional array

Multidimensional Arrays 1 2 3 4 An array

Multidimensional Arrays 0 1 2 3 4 5 COLUMNS 1 2 3 An array of arrays ROWS

Multidimensional Array Simplest way to define a multi-dimensional array is int matrix[4][6]; This would create a two-dimensional array of type int with 4 rows and 6 columns int matrix[4][6]={0};

Multidimensional Arrays 0 1 2 3 4 5 matrix COLUMNS 1 2 3 An array of arrays ROWS

Accessing a 2D Array matrix[2][3]=22; matrix[0][5]=44; 44 22 0 1 2 3 4 5 matrix 44 22 1 2 3

Processing a 2D Array w/Loop for(k=0; k<6; k++) matrix[3][k]=k; 0 1 2 3 4 5 matrix 44 22 1 2 3 4 5 1 2 3

2D Array Read/Print Example #include<iostream.h> void read(int a[][5]); //Read the input into two dimen array a void print(const int a[][5]);//Print array a void main() { int a[3][5]; read(a); print(a); } void read(int a[][5]) { cout << "Enter 15 integers, 5 per row:\n"; for (int i=0; i<3; i++) { for (int j=0; j<5; j++) cin >> a[i][j];

2D Array Example (contd) void print(const int a[][5]) { for (int i=0; i<3; i++) { cout << "Row " << i << ": "; for (int j=0; j<5; j++) cout << " " << a[i][j]; cout << endl; }

That’s a wrap ! What we learned today: Sorting Arrays Bubble Sort Selection Sort Multidimensional arrays

Go back home proud ! You’re brighter than Ar’ray’ !