Midterm Review CS112 Fall 2003. Problem 1 You are a manager. You must implement some functionality. A number of your subordinates bring to you a number.

Slides:



Advertisements
Similar presentations
IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
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.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
Sorting Part 2 CS221 – 3/4/09. Announcements Midterm: 3/11 – 15% of your total grade – We will review in class on 3/9 – You can bring one sheet of paper.
Simple Sorting Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
Time Complexity s Sorting –Insertion sorting s Time complexity.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Computer Science CS 330: Algorithms Quick Sort Gene Itkis.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
1 Lab Session-IX CSIT121 Fall 2000 w Arrays and Their Usage w Examples and Lab Exercises w Passing Arrays to Functions w Examples and Exercises w Sorting.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
Sorting, Stacks, Queues Bryce Boe 2013/11/06 CS24, Fall 2013.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Elementary Sorting Algorithms COMP s1 Sedgewick Chapter 6.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
7.2 Priority Queue ADTs Priority queue concepts
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Asymptotic Notation (O, Ω, )
CSC 211 Data Structures Lecture 13
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
CS 307 Fundamentals of Computer Science 1 bubble sort  Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to.
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.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
CS61B Spring’15 Discussion 11.  In-Place Sort: ◦ Keeps sorted items in original array (destructive) ◦ No equivalent for linked list-based input  Stable.
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
Bubble sort and comparison of elementary methods.
3 – SIMPLE SORTING ALGORITHMS
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Sorting algorithms: elementary advanced Sorting Data Structures and Algorithms in Java, Third EditionCh09 – 1.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Divide & Conquer Themes –Reasoning about code (correctness and cost) –iterative code, loop invariants, and sums –recursion, induction, and recurrence relations.
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.
Sorting  Selection Sort  Bubble Sort  Insertion Sort  Merge Sort (chap. 14)  Quick Sort (chap. 14)  Heap Sort (chap. 9)
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Sorting.
Heaps (8.3) CSE 2011 Winter May 2018.
Midterm Review.
7.1 What is a Sorting Network?
Week 6 - Wednesday CS221.
Programming Abstractions
Week 12 - Wednesday CS221.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
8/04/2009 Many thanks to David Sun for some of the included slides!
Programming Abstractions
Linked List and Selection Sort
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
David Kauchak cs161 Summer 2009
Heaps By JJ Shepherd.
Applications of Arrays
Presentation transcript:

Midterm Review CS112 Fall 2003

Problem 1 You are a manager. You must implement some functionality. A number of your subordinates bring to you a number of proposed implementation methods. The corresponding performance characteristics are given below. –Order these in order of your preference, best choice first (assume your applications require very large n): A(n) =  (n 3 lg n / 2 n ) = O(1) o(1)1 B(n) = n 3 / lg n =  (n 3 /lg n)5 C(n) = 1000 n lg n =  (n lg n)3 D(n) = 5 n 2 =  (n 2 )4 E(n) = 100 n =  (n)2 –One of the engineers advocates an algorithm for which he can only show that it is  (lg n). What can you say about it as compared to the above? Definitely worse than A. All others can be either. –Another engineer advocates another algorithm for which he can only show that it is O(n2). What can you say about it as compared to those in ‎a)? [do not confuse O and  ] Definitely, better than B. All others can be either.

Problem 2 Assume that linked lists are implemented using the following declaration class ListNode {public: int data; ListNode * next; }; Consider the following function: void grow(ListNode * L) { p = L; while(p != NULL) { while((p->next!=NULL)&&(p->data next->data)) p->next = p->next->next; p = p->next; } Let L be a pointer on the following linked list: L p

Problem 3 How fast each of the following sorting algorithms work on the input of n elements given in –the correct sorted order (e.g. 1,2,3,4,5,6,7,8) Bubble:  (n)n-1 Insertion:  (n)n-1 Selection:  (n 2 )n(n-1)/2 Heap:  (n lg n)< 3n lg n –in the reversed sorted order (e.g. 8,7,6,5,4,3,2,1) 2Bubble:  (n 2 )n(n-1)/2 2Insertion:  (n 2 )n(n-1)/2 Selection:  (n 2 ) Heap:  (n lg n)

Problem 3 How fast each of the following sorting algorithms work on the input of n elements given in –in almost correct order: the largest element is in the first position, (e.g. 8,1,2,3,4,5,6,7) Bubble:  (n)2n-3 Insertion:  (n)2n-3 Selection:  (n 2 )n(n-1)/2 Heap:  (n lg n)< 3n lg n –as above, except the smallest element is in the last position, (e.g. 2,3,4,5,6,7,8,1) Bubble:  (n 2 )n(n-1)/2 Insertion:  (n)2n-3 Selection:  (n 2 )n(n-1)/2 Heap:  (n lg n)< 3n lg n