Bubble Trouble A statistical analysis of the Bubble Sort.

Slides:



Advertisements
Similar presentations
SortingTechniques. Bubble-sort: One of the simplest sorting methods. The basic idea is the weight of the record. The records are kept in an array held.
Advertisements

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Selection and Insertion Sort Mrs. C. Furman October 1, 2008.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
the fourth iteration of this loop is shown here
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Simple Sorting Algorithms
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
C++ for Engineers and Scientists Third Edition
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
CSC220 Data Structure Winter
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Searching and Sorting Chapter 9.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Analysis of Algorithms
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSC 211 Data Structures Lecture 13
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Sorting List is rearranged into sorted order How is the sorted order determined? – The ItemType is responsible for determining the key to be used in comparison.
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.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Bubble Sort.
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.
3 – SIMPLE SORTING ALGORITHMS
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Sorting Algorithms: Selection, Insertion and Bubble.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
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.
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
Searching Arrays Linear search Binary search small arrays
Sorting Why? Displaying in order Faster Searching Categories Internal
Algorithm design and Analysis
Bubble Sort The basics of a popular sorting algorithm.
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Bubble Sort Key Revision Points.
Searching.
Presentation transcript:

Bubble Trouble A statistical analysis of the Bubble Sort.

The Guys Who Bring It To You Jon Kroening Nick Jones Erik Weyers Andy Schieber Sean Porter

History and Background Jon Kroening

The Bubble Sort Popular algorithm used for sorting data Iverson was the first to use name ‘bubble sort’ in 1962, even though used earlier Unfortunately it is commonly used where the number of elements is too large

The Bubble Sort Starts at one end of the array and make repeated scans through the list comparing successive pairs of elements If the first element is larger than the second, called an inversion, then the values are swapped

The Bubble Sort Each scan will push the maximum element to the top

The Bubble Sort This is the “bubbling” effect that gives the bubble sort its name This process is continued until the list is sorted The more inversions in the list, the longer it takes to sort

Bubble Sort Algorithm template void bubbleSort(vector & v) { int pass, length; R temp; for( length = 0; length < v.size()-1; length++) { for(pass = 0; pass < v.size()-1; pass ++) { if(v[pass] > v[pass +1]) { temp = v[pass]; v[pass] = v[pass + 1]; v[pass + 1] = temp; }

Best and Worst Case Scenarios Nick Jones

Best Case of the Bubble Sort Let X: number of interchanges (discrete). Consider list of n elements already sorted x 1 < x 2 < x 3 < … < x n

Best Case (cont.) Only have to run through the list once since it is already in order, thus giving you n-1 comparisons and X = 0. Therefore, the time complexity of the best case scenario is O(n).

Worst Case of the Bubble Sort Let X: number of interchanges (discrete). Consider list of n elements in descending order x 1 > x 2 > x 3 > … > x n.

Worst Case (cont.) # of comparisons = # of interchanges for each pass. X = (n-1) + (n-2) + (n-3) + … This is an arithmetic series.

Worst Case (cont.) (n-1) + (n-2) + … = n(n-1)/2 So X = n(n-1)/2. Therefore, the time complexity of the worst case scenario is O(n 2 ).

Time Complexity Model of the Worst Case # elements Time in seconds

Average Case Nick Jones, Andy Schieber & Erik Weyers

Random Variables A random variable is called discrete if its range is finite or countably infinite. Bernoulli Random Variable – type of discrete random variable with a probability mass function p(x) = P{X = x}

Bernoulli Random Variable X is a Bernoulli Random Variable with probability p if p x (x) = p x (1-p) 1-x, x = {0,1}. Other notes: E[X] = p

Expected Value The expected value of a discrete random variable is

Sums of Random Variables If are random variables then i.e. expectation is linear

Analytic Estimates for Bubble Sort I = number of inversions X = number of interchanges Every inversion needs to be interchanged in order to sort the list, therefore X must be at least I, E[X] = The average number of inversions

and

The probability that i, j are inverted is.5 I is a Bernoulli random variable, therefore there are terms in the above sum

Since p(I) =.5 and there are terms in the above sum then

So The average case is equivalent to the worst case

Simulation Results Jon Kroening, Sean Porter, and Erik Weyers

int MAX = 99; int Random[MAX]; int temp, flag; for(int i = 0; i<100; i++) { flag = 1; // Random Number temp = 1+(int) (100.0*rand()/(RAND_MAX+1.0)); //obtained for(int check = 0; check <= i; check++){ if(Random[check] == temp) //Checks if number is flag = 0; // already obtained } if(flag == 0) //if number is already obtained i--; //then it will reloop to find new one else{ Random[i] = temp; //Otherwise enters number cout << Random[i] << endl; //in array and displays }

Simulation Results

Worst Case :

Closing Comments The Bubble sort is not efficient, there are other sorting algorithms that are much faster

A Final Note Thank you Dr. Deckleman for making this all possible

References “Probability Models for Computer Science” by Sheldon Ross, Academic Press 2002