18 April, 2000 CS1001 Lecture 20 Quizzes 3 and 4 review Application of Arrays –Stacks (continuation) –Sorting.

Slides:



Advertisements
Similar presentations
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Advertisements

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.
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
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.
Visual C++ Programming: Concepts and Projects
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Introduction to C Programming CE Lecture 11 Sorting and Searching using Arrays.
Bubble Sort Notes David Beard CS181. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.
CHAPTER 11 Sorting.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Programming Logic and Design Fourth Edition, Comprehensive
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Lecture 08 Sorting. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
INFO Problem Solving and Programming Logic INFO Problem Solving and Programming Logic Arrays Sorting.
Problem Solving and Algorithms
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
CSE 373 Data Structures and Algorithms
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
CP104 Introduction to Programming Array Lecture 25 __ 1 Josephus Problem In the Jewish revolt against Rome, Josephus and 39 of his comrades were holding.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Sorting Dr. Yingwu Zhu. Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order Ascending or descending Some O(n.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
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.
Sorting  Sorting Problem:  A list of items:  x1, x2, x3, …., xn  Arranging the list in ascending order  X1
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
5.3 Sorting Techniques. Sorting Techniques Sorting is the process of putting the data in alphabetical or numerical order using a key field primary key.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
Sorting Sorting takes an unordered array and makes it an ordered one
The Sorting Methods Lecture Notes 10. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. –
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
Sorting Arrays ANSI-C. Selection Sort Assume want to sort a table of integers, of size length, in increasing order. Sketch of algorithm: –Find position.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Required Background for ECOE 556. What do I need to catch up with? Basic data structures (Chapter 10) –Stack –Queue –Linked list –Array –Records (e.g.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Stacks and Queues Chapter 4.
Presentation transcript:

18 April, 2000 CS1001 Lecture 20 Quizzes 3 and 4 review Application of Arrays –Stacks (continuation) –Sorting

18 April, 2000 Quiz 3 Write a function that, given an integer N, calculate N! N! is N * ( N - 1 ) * ( N - 2 ) *... * 1 INTEGER FUNCTION Factorial(N) INTEGER INTENT(IN) :: N INTEGER :: product product = 1 DO I= 2, N product = product * I END DO factorial = product END FUNCTION Factorial C = M!/(R! * (M-R)!) ==> in Fortran: C = Factorial(M) / ( Factorial(R)*Factorial(M-R) ) product = 1 loop:Iproduct etc.

18 April, 2000 QUIZ 4, #1 1.) What will be displayed when the following code segment executes? INTEGER :: i, j INTEGER, DIMENSION (10) :: Table DO i = 1, 10 Table(i) = i+1 END DO DO i = 1, 4 j = i * print *, i, j, Table(j) END DO First DO loop: iTable(i) Second DO loop: ijTable(j)

18 April, 2000 Quiz 4, #2 Assume that Nums is an array of 100 integers, and that a program has read in values for all 100 array elements. Write a fragment of Fortran code that will calculate and display the number of elements in the array Nums that contain the value -1. Count = 0 DO I = 1,100 IF (Nums (I).EQ. –1) Count = Count +1 END DO PRINT *, Count

18 April, 2000 Stack A data structure used to implement Last-in-first- out (LIFO) processes –e.g.: stack of trays in the cafeteria Top : Stack Stack(1) Stack(top) Stack(4) Stack(3) Stack(2) : Two operations: –Push –Pop Some design issues: –How long (or deep) should the stack be (i.e., the dimension of the stack)? – When push, is there room in array? –When pop, is the stack empty ?

18 April, 2000 More practice with Arrays Sorting - arranging the elements in a list so that they are in ascending or descending order Consider simple selection sort: e.g., sort a list of integers in ascending –list represented in an one-dimensional array, say n integer numbers: x(1), x(2),...,x(n). Modify this list so the numbers are in ascending order –find the smallest number and put it in the first position –find the second smallest number and put it in the second position –etc. –after n-1 times through, the list is in ascending order

18 April, 2000 Selection Sort Starting: x(1)67 x(2)33 x(3)21 x(4)84 x(5)49 x(6)50 x(7)75 First time: x(1)67 x(2)33 x(3)21 x(4)84 x(5)49 x(6)50 x(7)75 Second time: x(1)21 x(2)33 x(3)67 x(4)84 x(5)49 x(6)50 x(7)75 Third time: x(1)21 x(2)33 x(3)67 x(4)84 x(5)49 x(6)50 x(7)75 Fourth time: x(1)21 x(2)33 x(3)49 x(4)84 x(5)67 x(6)50 x(7)75 Fifth time: x(1)21 x(2)33 x(3)49 x(4)50 x(5)67 x(6)84 x(7)75 Sixth time: x(1)21 x(2)33 x(3)49 x(4)50 x(5)67 x(6)84 x(7)75 Finish: x(1)21 x(2)33 x(3)49 x(4)50 x(5)67 x(6)75 x(7)84

18 April, 2000 SUBROUTINE SelectionSort(Item) INTEGER, DIMENSION(:), INTENT(INOUT) :: Item INTEGER :: NumItems, SmallestItem, I INTEGER, DIMENSION(1) :: MINLOC_array NumItems = SIZE(Item) DO I = 1, NumItems - 1 ! Find smallest item in the sublist ! Item(I),..., Item(NumItems) SmallestItem = MINVAL(Item(I:NumItems)) MINLOC_array = MINLOC(Item(I:NumItems)) LocationSmallest = (I - 1) + MINLOC_array(1) ! Interchange smallest item with Item(I) at ! beginning of sublist Item(LocationSmallest) = Item(I) Item(I) = SmallestItem END DO END SUBROUTINE SelectionSort

18 April, 2000 Bubble Sort Starting: x(1)67 x(2)33 x(3)21 x(4)84 x(5)49 x(6)50 x(7)75 First time: Second time: Finish: x(1)21 x(2)33 x(3)49 x(4)50 x(5)67 x(6)75 x(7)

18 April, 2000 SUBROUTINE BubbleSort(Item) INTEGER, DIMENSION(:), INTENT(INOUT) :: Item INTEGER :: NumPairs, LastSwap, I, Temp NumPairs = SIZE(Item) - 1 DO IF (NumPairs == 0) EXIT ! If no more pairs to check, terminate repetition ! Otherwise scan the sublist of the first NumPairs pairs ! in the list, interchanging items that are out of order LastSwap = 1 DO I = 1, NumPairs IF (Item(I) > Item(I+1)) THEN ! Items out of order -- interchange them Temp = Item(I) Item(I) = Item(I+1) Item(I+1) = Temp ! Record position of last swap LastSwap = I END IF END DO NumPairs = LastSwap - 1 END DO END SUBROUTINE BubbleSort