Chapter 7 Single-Dimensional Arrays

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays (continued)
Abstract Data Types (ADTs) Data Structures The Java Collections API
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 7 Single-Dimensional.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
CIS265/506 Cleveland State University – Prof. Victor Matos
Sort Algorithm.
Lecture 5 of Computer Science II
Chapter 7: Single-Dimensional Arrays
Data Structures I (CPCS-204)
Chapter 7 Single-Dimensional Arrays
Lecture 3 Linear Search and Binary Search ArrayLists
Chapter 6 Arrays DDC 2133 Programming II.
Chapter 10 Lists.
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Sorting Algorithms: Selection, Insertion and Bubble
Chapter 6 Arrays.
Chapter 6 Arrays.
CS Week 7 Jim Williams, PhD.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Arrays Data structures Related data items of same type
Chapter 6 Arrays.
Chapter 6 Arrays Solution Opening Problem
Mr. Dave Clausen La Cañada High School
Chapter 6 Arrays.
Selection sort Given an array of length n,
Chapter 10 Lists.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays and C-Strings
Chapter 6 Arrays.
Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 23 Searching and Sorting
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
Chapter 6 Arrays.
Chapter 5 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 19 Searching, Sorting and Big O
Insertion Sort and Shell Sort
Applications of Arrays
Presentation transcript:

Chapter 7 Single-Dimensional Arrays

Sorting Arrays Sorting, like searching, is also a common task in computer programming. Many different algorithms have been developed for sorting. This section introduces a simple, intuitive sorting algorithms: selection sort.

Selection Sort Selection sort finds the smallest number in the list and places it first. It then finds the smallest number remaining and places it second, and so on until the list contains only a single number.

From Idea to Solution list[0] list[1] list[2] list[3] ... list[10] for (int i = 0; i < list.length; i++) { select the smallest element in list[i..listSize-1]; swap the smallest with list[i], if necessary; // list[i] is in its correct position. // The next iteration apply on list[i+1..listSize-1] } list[0] list[1] list[2] list[3] ... list[10] list[0] list[1] list[2] list[3] ... list[10] list[0] list[1] list[2] list[3] ... list[10] list[0] list[1] list[2] list[3] ... list[10] list[0] list[1] list[2] list[3] ... list[10] ... list[0] list[1] list[2] list[3] ... list[10]

Wrap it in a Method public static void selectionSort(double[] list) { /** The method for sorting the numbers */ public static void selectionSort(double[] list) { for (int i = 0; i < list.length; i++) { // Find the minimum in the list[i..list.length-1] double currentMin = list[i]; int currentMinIndex = i; for (int j = i + 1; j < list.length; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; } // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; Invoke it selectionSort(yourList)

The Arrays.sort Method Since sorting is frequently used in programming, Java provides several overloaded sort methods for sorting an array of int, double, char, short, long, and float in the java.util.Arrays class. For example, the following code sorts an array of numbers and an array of characters. double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers);   char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; java.util.Arrays.sort(chars); Java 8 now provides Arrays.parallelSort(list) that utilizes the multicore for fast sorting.