Agenda About the homework EvensAndOdds Array class Array Sorting

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

Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Memory Layout for a Matrix class Matrix { double ** contents; int numRows, numCols; … } to initialize: contents = new double * [numRows]; for(int i=0;i
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
CSE 373 Data Structures and Algorithms
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Decision Maths 1 Sorting Algorithm Shuttle Sort A V Ali : 1.Compare items 1 and 2; swap them if necessary 2.Compare 2 and 3; swap.
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 an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
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 & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
In the first pass, the first two numbers are compared. The shuttle sort compares the numbers in pairs from left to right exchanging when necessary.
Dr. Sajib Datta Feb 14,  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Sort Algorithm.
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Arrays
Discrete Mathematics Algorithms.
Chapter 9: Searching, Sorting, and Algorithm Analysis
3.1 Algorithms (and real programming examples).
Sorting Algorithms.
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Algorithm Analysis and Big Oh Notation
Insertion Sort Sorted Unsorted
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Introduction to Search Algorithms
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.
Introduction to Programming
Shuttle Sort Example 1st pass Comparisons: 1
Sorting Algorithms.
And now for something completely different . . .
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
CSc 110, Spring 2017 Lecture 39: searching and sorting
Straight Selection Sort
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
Searching and Sorting Topics Sequential Search on an Unordered File
Intro to Sorting Sorting
Principles of Computing – UFCFA3-30-1
CS 1430: Programming in C++.
Searching and Sorting Arrays
Algorithm Analysis and Big Oh Notation
Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort.
Searching and Sorting Arrays
slides adapted from Marty Stepp
Sorting Algorithms.
Programming with Arrays 2
Introduction to Sorting Algorithms
Shuttle Sort Example 1st pass Comparisons: 1
Principles of Computing – UFCFA3-30-1
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Insertion Sort Array index Value Insertion sort.
Sorting Algorithms.
Presentation transcript:

Agenda About the homework EvensAndOdds Array class Array Sorting Bubble sort Selection sort practice

Remarks on the homework If you have 2 arrarys, one for even, one for odds, you will find some elements in both arrays have no data and initialized to 0. How to make the program better?

Array class Built in class and ready for use Provided in the java.util.Arrays Sort method(function) syntax Arrays.sort(<array name>); BinarySearch syntax Arrays.binarySearch(<arry>, key); => for this to work right, precondition: Array has to be sorted in ascending order

Practice int[] bArry = {16, 2, 31, 9}; System.out.println("array before sorting: "); for(int dummy : bArry) System.out.print(dummy+ " "); System.out.println(" "); Arrays.sort(bArry);

Bubble Sort 4 2 5 1 3 Original data 4 2 5 1 3 Compare the shaded pair. 4>2, so swap 2 4 5 1 3 Swap completed Compare the shaded pair. No swap needed 2 4 5 1 3 2 4 5 1 3 Compare the shaded pair. 5>1, so swap 2 4 1 5 3 Swap completed. 2 4 1 5 3 Compare the shaded pair. 5>3, so swap 2 4 1 3 5 Swap completed

selection Sort 4 2 5 1 3 Original data 1st pass: select smallest value in gray area just above.. It’s 1. Then 1 and 4 have now been swapped. 11 2 5 4 3 2nd pass: select smallest value in gray area just above.. It’s 2. No swap necessary since the 2 above is less than 3. 11 2 5 4 3 3rd pass: select smallest value in gray area just above.. It’s 3. Then 3 and 5 have now been swapped. 11 2 3 4 5 4th pass: select smallest value in gray area just above.. It’s 5. No swap necessary since the 4 is less than 5. 11 2 3 4 5

Swap method Original values p = 5; q = 97; Swapped values p = 97; q=5; How? temp = p; p = q; q = temp; temp 5 <= p is 5 p <= q is 97 97 q <= temp 5

selection Sort pseudocode for(arrayIndex = 0 to numItems -1 ) { for(subarrayIndex = arrayIndex to numItems-1) if (items[subarrayIndex] < items[arrayIndex]) swap items[arrayIndex] and items[arrayIndex] }

Practice Run the selection sort coded on the last slide with the Tester class on page 41. Turn in the work before the break