Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
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.
Garfield AP Computer Science
Computer Organization CS224
Understanding the Need for Sorting Records
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
CS0007: Introduction to Computer Programming Array Algorithms.
CSE 373: Data Structures and Algorithms
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Searching and Sorting Chapter 9.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 7 Problem Solving and Algorithms. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled.
Comparison of Optimization Algorithms By Jonathan Lutu.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
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.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
COP 3540 Data Structures with OOP
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CSCI 51 Introduction to Programming March 10, 2009.
Searching and Sorting Searching algorithms with simple arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
Sorting Mr. Jacobs.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Sorting Why? Displaying in order Faster Searching Categories Internal
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Algorithm design and Analysis
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.
Bubble, Selection & Insertion sort
Quicksort analysis Bubble sort
Searching.
Search,Sort,Recursion.
Module 8 – Searching & Sorting Algorithms
Sorting Popular algorithms:
Presentation transcript:

Computing and the Web Algorithmic Thinking

Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer environment n Implementing the algorithm n Different Language Choices n The Algorithm in Action

Understanding a Specific Process n Putting items in order –Sorting –Alphabetizing n Methods –Compare and Swap –Form Multiple Piles –Divide and Conquer

Developing An Algorithm n A simplified approach n Given index cards with a number on each –A small number of cards is easy to handle –A large number of cards is awkward –You can’t see all of the cards at the same time n We need a methodical way of dealing with any number of cards

Developing An Algorithm n Given only two hands and a pile of cards: –Pass through the pile holding out the highest number –Repeat the process over and over again n A common name for this is a “Bubble Sort” n Doesn’t require a complex algorithm

The Computer Environment n The computer memory can hold the index cards (rather their contents) n We can set aside memory and give it a name n We can reference memory and manipulate the contents of it

The Computer Environment n When we reference the name we mean all of the related memory locations n Each name must be unique n We can define how much memory is set aside for each name n We call the names variables

The Computer Environment n We can create arrays of similar items n Items can be referenced by their “number” n Array elements can be referenced by their name and element number –Item(3) = 27

The Computer Environment n We can use another variable to reference an element in an array –j = 5 –Item(j) = 27 n The variable j is a “pointer”

Implementing the algorithm n The index cards can be thought of as an array n Your hands function like a pointer n The algorithm was to compare and swap as we work our way through the pile

Implementing the algorithm n Cards = array(1 to 18) n Current = 1 n Next = Current + 1 n Compare Cards(Current) and Cards(Next) –Swap them if they aren’t in order n Increment Current and Next

Implementing the algorithm Dimension Cards(18) Current = 1 Do count = 1 to 18 Next = Current + 1 Next = Current + 1 If (Cards(Current) > Cards(Next)) Then Do If (Cards(Current) > Cards(Next)) Then Do Temp = Cards(Current) Temp = Cards(Current) Cards(Current) = Cards(Next) Cards(Current) = Cards(Next) Cards(Next) = Temp Cards(Next) = Temp End End Current = Current + 1 Current = Current + 1End

Implementing the algorithm Dimension Cards(18) Current = 1 Do again = 1 to 18 Do count = 1 to 18 Do count = 1 to 18 Next = Current + 1 Next = Current + 1 If (Cards(Current) > Cards(Next)) Then Do If (Cards(Current) > Cards(Next)) Then Do Temp = Cards(Current) Temp = Cards(Current) Cards(Current) = Cards(Next) Cards(Current) = Cards(Next) Cards(Next) = Temp Cards(Next) = Temp End End Current = Current + 1 Current = Current + 1 End EndEnd

Different Language Choices n The “C” language /* bubble sort the array */ for (x=0; x < MAX-1; x++) for (y=0; y < MAX-x-1; y++) for (y=0; y < MAX-x-1; y++) if (a[y] > a[y+1]) if (a[y] > a[y+1]) { t=a[y]; t=a[y]; a[y]=a[y+1]; a[y]=a[y+1]; a[y+1]=t; a[y+1]=t; } /* print sorted array */ printf(" \n"); for (i=0; i < MAX; i++) printf("%d\n",a[i]);

Different Language Choices n The “Fortran” language DIMENSION NUMS(15) C C SORT THE ARRAY USING BUBBLE SORT. 40 N = N ISCAN = 1 IOK = 1 ISTOP = N IF(ISTART - ISTOP) 50, 110, IF(NUMS(ISCAN) - NUMS(ISCAN+1)) 90,90,60 C C SWAP 60 J = NUMS(ISCAN) NUMS(ISCAN) = NUMS(ISCAN+1) NUMS(ISCAN+1) = J IOK = 0 C C TIME TO ISTART OVER? 90 IF(ISCAN - (ISTOP - 1)) 80,100,100 C C NEXT PAIR 80 ISCAN = ISCAN + 1 GOTO 50 C C NEXT ISCAN 100 IF(IOK) 105,105, ISTOP = ISTOP - 1 GOTO 70 C C PRINT 110 DO 120 I=1, N 120 PRINT 130, NUMS(I) 130 FORMAT(I10) STOP END

Different Language Choices n The “Java” language This version of bubble sort makes a fixed number of passes (length of the array - 1). Each inner loop is one shorter than the previous one. public static void bubbleSort2(int[] x) { boolean doMore = true; while (doMore) { doMore = false; // assume this is last pass over array for (int i=0; i<x.length-1; i++) { if (x[i] > x[i+1]) { // exchange elements int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; doMore = true; // after an exchange, must look again }

Modified Algorithms Bubble Sort -- stop when no exchanges, shorter range each time This version of bubble sort combines ideas from the previous versions. It stops when there are no more exchanges, and it also sorts a smaller range each time. public static void bubbleSort3(int[] x) { int n = x.length; boolean doMore = true; while (doMore) { n--; doMore = false; // assume this is our last pass over the array for (int i=0; i<n; i++) { if (x[i] > x[i+1]) { // exchange elements int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; doMore = true; // after an exchange, must look again } }//end method bubbleSort3

Modified Algorithms Bubble Sort -- Sort only necessary range After a pass on bubble sort, it's only necessary to sort from just below the first exchange to just after the last exchange, because everything that wasn't exchanged must be in the correct order. This version of bubble sort remembers the lowest and highest locations where there was an exchange, and sorts only that part of the array. Although it is a little more complicated, it is more efficient than the other bubble sorts. public static void bubbleSort4(int[] x) { int newLowest = 0; // index of first comparison int newHighest = x.length-1; // index of last comparison while (newLowest < newHighest) { int highest = newHighest; int lowest = newLowest; newLowest = x.length; // start higher than any legal index for (int i=lowest; i<highest; i++) { if (x[i] > x[i+1]) { // exchange elements int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; if (i<newLowest) { newLowest = i-1; if (newLowest < 0) { newLowest = 0; } } else if (i>newHighest) { newHighest = i+1; } }//end method bubbleSort4

The Algorithm in Action Demo/example1.html Demo/example1.html