Searching & Sorting in Java Bubble Sort

Similar documents
SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

CSCI 261 Computer Science II

CS 106 Introduction to Computer Science I

Searching & Sorting in Java Shell Sort

C/C++ Programming Lecture 18 Name:

Chapter 10 - Notes Applications of Arrays

Sorting. Task Description. Selection Sort. Should we worry about speed?

Algorithm Analysis. Performance Factors

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

Sorting Algorithms part 1

Search,Sort,Recursion

Elementary Sorting Algorithms

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Lecture 6 Sorting and Searching

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Data Structures and Algorithms Sorting

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for

Searching and Sorting

LECTURE 17. Array Searching and Sorting

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Sorting. Weiss chapter , 8.6

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Sorting Pearson Education, Inc. All rights reserved.

LINEAR INSERTION SORT

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Sorting: Overview/Questions

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

public static <E extends Comparable<? super E>>void BubbleSort(E[] array )

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

Chapter 8 Search and Sort

CS 2412 Data Structures. Chapter 10 Sorting and Searching

UE Algorithmen und Datenstrukturen 1 UE Praktische Informatik 1. Übung 9. Sorting

STUDENT LESSON A17 Quadratic Sorting Algorithms

for (int outercounter = nums.length - 1; outercounter > 0 && swappedthatturn; outercounter --

Data Structures and Algorithms

COT 6405: Analysis of Algorithms. Giri Narasimhan. ECS 389; Phone: x3748

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

CP222 Computer Science II. Searching and Sorting

Chapter 7 Sorting. Terminology. Selection Sort

Walls & Mirrors Chapter 9. Algorithm Efficiency and Sorting

UNIT 7. SEARCH, SORT AND MERGE

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

SORTING. How? Binary Search gives log(n) performance.

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Sorting. Introduction. Classification

CSE 373. Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort reading: Weiss Ch. 7. slides created by Marty Stepp

LECTURE 08 SEARCHING AND SORTING ARRAYS

Unit 10: Sorting/Searching/Recursion

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Lecture 2: Sorting and the Big O. Wednesday, 16 September 2009

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

Searching and Sorting (Savitch, Chapter 7.4)

CSCI 2170 Algorithm Analysis

What is an algorithm?

Data Structures and Algorithms for Engineers

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Programming II (CS300)

SORTING AND SEARCHING

[1] Strings, Arrays, Pointers, and Hashing

Sorting is ordering a list of objects. Here are some sorting algorithms

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Data Structures and Algorithms (DSA) Course 12 Trees & sort. Iulian Năstac

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

Question And Answer.

Giri Narasimhan. COT 5993: Introduction to Algorithms. ECS 389; Phone: x3748

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

Chapter 8 Algorithms 1

CS 12 Fall 2003 Solutions for mid-term exam #2

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Bubble sort starts with very first two elements, comparing them to check which one is greater.

Sorting (Weiss chapter )

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1 Algorithm and Proof for Minimum Vertex Coloring

Data Structures - Simple Sorting

School of Informatics, University of Edinburgh

Outline. What s wrong with Enumerations? Indexing Searching Self-Organizing Vectors Binary Searching Sorting Command line Arguments.

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

Introduction. Sorting. Table of Contents

CSE 143 Lecture 16 (B)

Data Structures and Abstractions with Java

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Sorting. Bubble sort method. Bubble sort properties. Quick sort method. Notes. Eugeniy E. Mikhailov. Lecture 27. Notes. Notes

CS 302: Introduction to Programming in Java. Lecture 12

Arrays and Other Data Types

Outline. applications of hashing equality and comparisons in Java sorting selection sort bubble sort insertion sort

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Transcription:

With the bubble sort, the basic idea is to compare adjacent values and exchange them if they are not in order. Consider the following example which shows the first pass through the algorithm. 1. Compare 6 and 3 6 3 5 8 2 2. 6 is higher, so swap 6 and 3 3 6 5 8 2 3. Compare 6 and 5 3 6 5 8 2 4. 6 is higher, so swap 6 and 5 3 5 6 8 2 5. Compare 6 and 8 3 5 6 8 2 6. 8 is higher, so no swap is performed 3 5 6 8 2 7. Compare 8 and 2 3 5 6 8 2 8. 8 is higher, so swap 8 and 2 9. The largest value, 8, is at the end of the array The result of this comparing and exchanging is that, after one pass, the largest value will be at the upper end of the list. Like a bubble, it has risen to the top. On our next pass, we can ignore this position and work with a shortened list, allowing the largest remaining value to rise to the (slightly lower) top. The complete bubble sort is shown below in a more condensed format. First Pass 6 3 5 8 2 3 6 5 8 2 3 5 6 8 2 3 5 6 8 2 Second Pass Third Pass 3 2 5 6 8 Fourth Pass 3 2 5 6 8 2 3 5 6 8 18. Apr. 2013 Page 1 of 5

To code this algorithm in Java, we start with a loop very similar to that used for the selection sort. for (int top = list.length 1; top > 0; top--) // compare adjacent values of the unsorted sublist // from 0 to top, exchanging as necessary It is worth noting that, on each pass, the exchanges tend to move all values toward their final positions in the list. Thus it is possible for the list to be sorted before the final pass is completed. For the sake of efficiency, we should stop working as soon as possible to avoid any unnecessary passes. In a sorted list, the bubble sort algorithm would not perform any exchanges, so we use a boolean flag to monitor this condition. The boolean value sorted is initialized to false so the initial pass will begin. At the beginning of each pass, the value is set to true, but if a single swap is required, it is set back to false. Processing continues until sorted stays true, or we have completed the maximum number of passes. The full method, for an array of strings, is as follows. public static void bubblesort (String[] list) boolean sorted = false; for (int top = list.length - 1; top > 0 && sorted == false; top--) sorted = true; for (int i = 0; i < top; i++) if (list[i].compareto(list[i+1]) > 0) sorted = false; // a swap was required In general, bubble sort is not recommended for any serious applications. It is almost always slower than insertion and selection sorts because it usually involves far more data movement than they require. 18. Apr. 2013 Page 2 of 5

Exercises 1. Show the comparisons and exchanges that would take place in using a bubble sort to put the following data in ascending order. 3 8 3 2 7 5 2. What changes would have to be made to the bubblesort method in order to make it sort values in descending order? 3. A variation of the bubble sort is the cocktail shaker sort in which, on odd-numbered passes, large values are carried to the top of the list. On even-numbered passes, small values are carried to the bottom of the list. Show the first two passes on the following data. 2 9 4 6 1 7 4. Write a method shakersort to implement the cocktail shaker sort algorithm to arrange an array of double values in ascending order. Use a boolean flag to stop processing once the items have been completely sorted. 18. Apr. 2013 Page 3 of 5

1. 2. 3. A variation of the bubble sort is the cocktail shaker sort in which, on odd-numbered passes, large values are carried to the top of the list. On even-numbered passes, small values are carried to the bottom of the list. Show the first two passes on the following data. 2 9 4 6 1 7 start 2 9 4 6 1 7 pass 1 2 4 6 1 7 9 pass 2 1 2 4 6 7 9 18. Apr. 2013 Page 4 of 5

4. Write a method shakersort to implement the cocktail shaker sort algorithm to arrange an array of double values in ascending order. Use a boolean flag to stop processing once the items have been completely sorted. New code is in blue. public static void shakersort (String[] list) boolean sorted = false; int pass = 1; // need to start at top-1 to allow for compare of last TWO array locations for (int top = list.length - 1; top > 0 && sorted == false; top--) sorted = true; // will be set back to false if swap required if (pass%2!= 0) // odd pass // move bigger values up, until biggest is at end of array for (int i = 0; i < top; i++) // if element i is bigger than (i+1), swap them if (list[i].compareto(list[i+1]) > 0) sorted = false; // a swap was required, so list is not sorted else // even pass // move smaller values down, until smallest is at beginning of array for (int i = top-1; i >=0; i--) // if element i+1 is smaller than i, swap them if (list[i+1].compareto(list[i]) < 0) sorted = false; // a swap was required, so list is not sorted 18. Apr. 2013 Page 5 of 5