Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.

Slides:



Advertisements
Similar presentations
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Advertisements

Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Searching and Sorting Linear Search Binary Search Selection Sort
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Two Ways to Store Data in a File Text format Binary format.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
QUICK SORT. QUICK SORT IS AWESOME Fast sorting algorithm Can be used to sort big data volumes Uses a divide and conquer strategy that can be multithreaded.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
1 Insertion sort [ ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ ] i=3 j=1 insert i=4 at j=0 [
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
The Selection Sort Mr. Dave Clausen La Cañada High School.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Sorting Arrays ANSI-C. Selection Sort Assume want to sort a table of integers, of size length, in increasing order. Sketch of algorithm: –Find position.
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.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
Chapter 10 Arrays.
Building Java Programs
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Chapter 10 Arrays.
Building Java Programs
Chapter 6 Arrays.
Building Java Programs Chapter 7
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.
Mr. Dave Clausen La Cañada High School
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Can we solve this problem?
Mr. Dave Clausen La Cañada High School
OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
Building Java Programs
Can we solve this problem?
Sorting and Searching -- Introduction
Building Java Programs
Building Java Programs
Presentation transcript:

Arrays and Sorting

Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array Output the sorted array

Text file containing integers Array of short (integers) numbers.txt numbers[]

Text file containing integers Array of short (integers) numbers.txtnumbers[10]

Text file containing integers Array of short (integers) numbers.txt numbers[10] inputFromFile(inputFileName, numbersArray);

private static int inputFromFile(String filename, short[] numbers){ TextFileInput in = new TextFileInput(filename); int lengthFilled = 0; String line = in.readLine(); while ( lengthFilled < numbers.length && line != null ) { numbers[lengthFilled++] = Short.parseShort(line); line = in.readLine(); } // while if ( line != null ) { System.out.println("File contains too many numbers."); System.out.println("This program can process only " + numbers.length + " numbers."); System.exit(1); } // if in.close(); return lengthFilled; } // method inputFromFile Main program calls subArrayLength = inputFromFile(inputFileName, numbersArray); Program 1.1

Text file containing integers Array of short (integers) numbers.txt numbers[10] subArrayLength is 5

numbers[10] subArrayLength 5 Partially-filled array for (int i =0;i<numbers.length; i ++) { for (int i =0;i<subArrayLength; i ++) { NO: YES:

numbers[10] subArrayLength 5 // average the numbers sum=0; for (int i =0; i<subArrayLength; i ++) sum += numbers[i]; Average = sum/subArrayLength;

numbers[10] subArrayLength 5 // find the smallest number smallest = numbers[0]; for (int i =1; i<subArrayLength; i ++) smallest = Math.min(smallest,numbers[i];

numbers[10] subArrayLength 5 // find the index of the smallest number indexLowest = 0; for ( int j = 1; j < subArrayLength; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j;

numbers[10] // find the index of the smallest number This is the basis of “Selection Sort”

numbers[10] find the index of the smallest number This is the basis of “Selection Sort” Find the smallest number and swap it with the number at the top of the array numbers[10]

numbers[10] sorted Not sorted; Find the smallest number here and swap it with numbers[1]

numbers[10] numbers[10] sorted

numbers[10] numbers[10] sorted

numbers[10] numbers[10] sorted

numbers[10] sorted

numbers[10] sorted

private static void selectionSort (short[] array, int length) { for ( int i = 0; i < length - 1; i++ ) { int indexLowest = i; for ( int j = i + 1; j < length; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j; if ( array[indexLowest] != array[i] ) { short temp = array[indexLowest]; array[indexLowest] = array[i]; array[i] = temp; } // if } // for i } // method selectionSort