Sorting an Array. Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Sorting algorithms Sieve of Eratosthenes
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
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.
One Dimensional Arrays
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Sorting Algorithms Selection, Bubble, Insertion and Radix Sort.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s name obeys the same rules as when declaring a variable:
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
1 Chapter 7 Sorting Sorting of an array of N items A [0], A [1], A [2], …, A [N-1] Sorting in ascending order Sorting in main memory (internal sort)
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
Searching Arrays Linear search Binary search small arrays
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Computer Memory 21 oneGrade integer We can declare, assign and manipulate individual variables with ease…
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.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
I Power Int 2 Computing Software Development High Level Language Constructs.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Bubble Sort.
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.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
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.
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting Sorting takes an unordered array and makes it an ordered one
Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion.
A: A: double “4” A: “34” 4.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Data Structures Arrays and Lists Part 2 More List Operations.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Chapter 7: Sorting Algorithms Insertion Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Searching Arrays Linear search Binary search small arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
Alternate Version of STARTING OUT WITH C++ 4th Edition
Recitation 13 Searching and Sorting.
Alg2_1c Extra Material for Alg2_1
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
Introduction to Programming
Sorting Algorithms.
And now for something completely different . . .
Sorting Algorithms.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Presentation transcript:

Sorting an Array

Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…

Why sort an Array? Consider findmin, findmax, findrepeat on unsorted and sorted arrays.

MergeSortQuickSortInsertionSortShellSortUSA_Sort (We will learn BubbleSort )

First we need to learn how to swap two numbers

Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB)

Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intA = intB intB = intA bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intA = intB intB = intA bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) Our first attempt is wrong!!

Dim intA As Integer = 1 Dim intB As Integer = 5 Dim intT As Integer bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intT = intA intA = intB intB = intT bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) Dim intA As Integer = 1 Dim intB As Integer = 5 Dim intT As Integer bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intT = intA intA = intB intB = intT bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) This works

shtS (short) Index0123 Values3291 We have seen how to swap two integers, let us see how to swap two elements of an array…

Dim intS() As Integer = {3, 2, 9, 1} Dim intT As Integer bntRedDemo.Text = "S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1)) intT = intS(0) intS(0) = intS(1) intS(1) = intT bntRedDemo.Text = "Now S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1)) Dim intS() As Integer = {3, 2, 9, 1} Dim intT As Integer bntRedDemo.Text = "S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1)) intT = intS(0) intS(0) = intS(1) intS(1) = intT bntRedDemo.Text = "Now S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1))

Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 bntRedDemo.Text = "Adjacent pairs" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Next intI Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 bntRedDemo.Text = "Adjacent pairs" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Next intI Note the “-1”

Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then bntRedDemo.Text = "Do Swap" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Else bntRedDemo.Text = "Don't Swap" & Str(intS(intI)) & "" & Str(intS(intI + 1)) End If Next intI Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then bntRedDemo.Text = "Do Swap" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Else bntRedDemo.Text = "Don't Swap" & Str(intS(intI)) & "" & Str(intS(intI + 1)) End If Next intI

Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI After running this code, we have intS = 2, 3, 1, 9

Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop After running this code, we have intS = 1, 2, 3, 9 !!

Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop After running this code, we have intS = 1, 2, 3, 9 !! In the next slides I will represent all this text by a red box

Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer Dim blnIsSorted As Boolean = False While Not (blnIsSorted) blnIsSorted = True For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) blnIsSorted = False End If Next intI End While Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer Dim blnIsSorted As Boolean = False While Not (blnIsSorted) blnIsSorted = True For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) blnIsSorted = False End If Next intI End While Swap 2 numbers