# In the example above, it is as though the elements are in two-dimensions such a table

Save this PDF as:

Size: px
Start display at page:

Download "In the example above, it is as though the elements are in two-dimensions such a table"

## Transcription

1 Multiple-Dimensional Arrays Elements of arrays may have more than one index, i.e. In example above, it is as though elements are in two-dimensions such a table with rows and columns Array Additional Searching dimensions and Sorting that go beyond two may be included as is needed CST242 A Sample 2-Dimensional Array Multiple-Dimensional Arrays Accessing Array Elements Elements of arrays may have more than one index, i.e. An array element is accessed by using row and column integers in two sets of brackets In example above, it is as though elements are in two-dimensions such a table with rows and columns Additional Integer variables dimensions may that be used go beyond to replace two eir may be or included both of as indexes, is needed i.e. mileage[start][end] Accessing Array Elements Declaring a 2-Dimensional Array An array element is accessed by using row and column integers in two sets of brackets The new array object is created using two sets of brackets in array declaration First bracketed value is conceptually number of rows Integer Second bracketed variables may value be is used conceptually to replace eir number or both of columns of indexes, i.e. mileage[start][end] int[][] mileage = new int[4][4]; Declaring a 2-Dimensional Array Initializing a 2-Dimensional Array in Declaration The new array object is created using two sets of brackets in array declaration First Each bracketed row is assigned value is in conceptually a separate subset number of command-delimited of rows brackets Second bracketed value is conceptually number of columns int mileage[][] = int[][] { mileage = new int[4][4]; { 0, 160, 390, 40}, Initializing {160, a 0, 2-Dimensional 240, 200}, Array in Declaration Each {390, row 240, is assigned 0, 440}, in a separate subset of command-delimited brackets { 40, 200, 440, 0} int }; mileage[][] = { MultidimensionalArrays.java (Page 1) { 0, 160, 390, 40}, MultidimensionalArrays.java {160, 0, 240, 200}, (Page 2) {390, 240, 0, 440}, MultidimensionalArrays.java (Page 3) { 40, 200, 440, 0} MultidimensionalArrays.java }; (Page 3) Searching and Sorting Searching data involves determining wher a value (referred to as search key) is present in data and, if so, finding its location. Two popular search algorithms are simple linear search and faster but more complex binary search Sorting places data in ascending or descending order, based on one or more sort keys The Arrays Class Class Arrays contains methods for manipulating arrays (such as sorting and searching) Also contains a static methods that allow arrays to be viewed as lists Found in java.util package, i.e. import java.util.arrays; The sort() Method of Class Arrays The sort method is a static method of Arrays class that sorts elements in an array in ascending order by default Overloaded version lets sort order to be changed Format: Arrays.sort(arrayObject);

2 import java.util.arrays; The sort() Method of Class Arrays The sort method is a static method of Arrays class that sorts elements in an array in ascending order by default Overloaded version lets sort order to be changed The Format: sort method is a static method of Arrays class that sorts elements in an array Arrays.sort(arrayObject); in ascending order by default Overloaded version lets sort order to be changed Format: Arrays.sort(value); Arrays.sort(arrayObject); The tostring() Method of Class Arrays The tostring method is a static method of class Arrays that displays array object as Arrays.sort(value); a list The Format: tostring() Method of Class Arrays The Arrays.toString(arrayObject); method is a static method of class Arrays that displays array object as a list Format: System.out.println( Arrays.toString(value) ); Arrays.toString(arrayObject); ArraysSort.java Linear System.out.println( Search Arrays.toString(value) ); Linear The linear Search search algorithm searches each element in an array sequentially. If search key does not match an element in array, algorithm tests each The linear search algorithm searches each element in an array sequentially. element, and when end of array is reached, informs user that search If key is search not present. key does not match an element in array, algorithm tests each element, and when end of array is reached, informs user that search If search key is in array, algorithm tests each element until it finds one key is not present. that matches search key and returns index of that element. If search key is in array, algorithm tests each element until it finds one If re are duplicate values in array, linear search returns index of first that matches search key and returns index of that element. element in array that matches search key. If re are duplicate values in array, linear search returns index of first element in array that matches search key. Searching algorithms all accomplish same goal finding an element that matches a given search key, if such an element does, in fact, exist. The major difference is amount of effort y require to complete search. Big O notation indicates worst-case run time for an algorithm that is, how hard an algorithm may have to work to solve a problem. For searching and sorting algorithms, this depends particularly on how many data elements re are. If an algorithm is completely independent of number of elements in array, it is said to have a constant run time, which is represented in Big O notation as O(1). An algorithm that is O(1) does not necessarily require only one comparison. O(1) just means that number of comparisons is constant it does not grow as size of array increases. An algorithm that requires a total of n 1 comparisons is said to be O(n). An O(n) algorithm is referred to as having a linear run time. O(n) is often pronounced on order of n or simply order n. Constant factors are omitted in Big O notation. Big O is concerned with how an algorithm s run time grows in relation to number of items processed. O(n 2 ) is referred to as quadratic run time and pronounced on order of n-squared or more simply order n-squared. When n is small, O(n 2 ) algorithms (running on today s computers) will not noticeably affect performance.

3 of items processed. O(n 2 ) is referred to as quadratic run time and pronounced on order of n-squared or more simply order n-squared. When n is small, O(n 2 ) algorithms (running on today s computers) will not noticeably affect performance. But as n grows, you ll start to notice performance degradation. An O(n 2 ) algorithm running on a million-element array would require a trillion operations (where each could actually require several machine instructions to execute). A billion-element array would require a quintillion operations. You ll also see algorithms with more favorable Big O measures. The linear search algorithm runs in O(n) time. The worst case in this algorithm is that every element must be checked to determine wher search item exists in array. If size of array is doubled, number of comparisons that algorithm must perform is also doubled. Linear search can provide outstanding performance if element matching search key happens to be at or near front of array. We seek algorithms that perform well, on average, across all searches, including those where element matching search key is near end of array. If a program needs to perform many searches on large arrays, it s better to implement a more efficient algorithm, such as binary search Binary Search The binary search algorithm is more efficient than linear search, but it requires that array be sorted. The first iteration tests middle element in array. If this matches search key, algorithm ends. If search key is less than middle element, algorithm continues with only first half of array. If search key is greater than middle element, algorithm continues with only second half. Each iteration tests middle value of remaining portion of array. If search key does not match element, algorithm eliminates half of remaining elements. The algorithm ends by eir finding an element that matches search key or reducing subarray to zero size Binary Search (cont.) In worst-case scenario, searching a sorted array of 1023 elements takes only 10 comparisons when using a binary search. The number 1023 (2 10 1) is divided by 2 only 10 times to get value 0, which indicates that re are no more elements to test. Dividing by 2 is equivalent to one comparison in binary search algorithm. Thus, an array of 1,048,575 (2 20 1) elements takes a maximum of 20 comparisons to find key, and an array of over one billion elements takes a maximum of comparisons to find key.

4 array, which is represented as log 2 n. All logarithms grow at roughly same rate, so in big O notation base can be omitted. This results in a big O of O(log n) for a binary search, which is also known as logarithmic run time. Sorting A difference Algorithms between an average of 500 million comparisons for linear search Sorting and a data maximum (i.e., placing of only data comparisons into some for particular binary order, search! i.e. ascending or descending) Binary is Search one of (cont.) most important computing applications The An important maximum item number to understand of comparisons about needed sorting for is that binary sorted search array of will any sorted be array same is no matter exponent which of algorithm first power you use of to 2 sort greater than array number of elements in The array, choice which of algorithm is represented affects only as log 2 n. run time and memory use of program All Selection logarithms Sort grow at roughly same rate, so in big O notation base can be omitted. The selection sort is a simple, but inefficient, sorting algorithm This results in a big O of O(log n) for a binary search, which is also known as Its first iteration selects smallest element in array and swaps it with first logarithmic run time. element. Sorting The second Algorithms iteration selects second-smallest item (which is smallest item of Sorting remaining data (i.e., elements) placing and swaps data into it with some particular second element order, i.e. ascending or Selection descending) Sort is one of most important computing applications An important item to understand about sorting is that sorted array will be The algorithm continues until last iteration selects second-largest element and same no matter which algorithm you use to sort array swaps it with second-to-last index, leaving largest element in last index The choice of algorithm affects only run time and memory use of program After th i iteration, smallest i items of array will be sorted into increasing Selection order in Sort first i elements of array: The After selection first sort iteration, is a simple, smallest but inefficient, element sorting is first algorithm position Its After first iteration second iteration, selects smallest two smallest element elements in array are in and order swaps in first it with two positions, first element. etc. The Selection second Sort iteration selects second-smallest item (which is smallest item of remaining elements) and swaps it with second element After first iteration, smallest element is in first position; after second iteration, Selection two smallest Sort elements are in order in first two positions, etc. The algorithm selection sort continues algorithm until runs in last O(n iteration 2 ) time. selects second-largest element and swaps it with second-to-last index, leaving largest element in last index After th i iteration, smallest i items of array will be sorted into increasing order in first i elements of array: After first iteration, smallest element is in first position After second iteration, two smallest elements are in order in first two positions, Insertion etc. Sort Selection The insertion Sort sort is anor simple although inefficient, sorting algorithm The first iteration takes second element in array and, if it s less than first After first iteration, smallest element is in first position; after second iteration, element, swaps it with first element two smallest elements are in order in first two positions, etc. The second iteration looks at third element The selection sort algorithm runs in O(n 2 and inserts it into correct position ) time. with respect to first two, so all three elements are in order Insertion At ithsort iteration of this algorithm, first i elements in original array will be The sorted insertion sort is anor simple although inefficient, sorting algorithm The first iteration takes second element in array and, if it s less than first element, swaps it with first element The second iteration looks at third element and inserts it into correct position InsertionSort.java with respect to first two, so (Page all three 2) elements are in order At ith iteration of this algorithm, first i elements in original array will be InsertionSort.java sorted (Page 3) Merge Sort (Page 1) The merge sort is an efficient sorting algorithm that conceptually is more complex than selection sort and insertion sorts Sorts an array by splitting it into two equal-sized sub-arrays, sorting each sub-array, n merging m into one larger array Merge Sort (Page 2)

5 41 selection sort and insertion sorts Sorts an array by splitting it into two equal-sized sub-arrays, sorting each sub-array, n merging m into one larger array Merge Sort (Page 2) The implementation of merge sort in this example is recursive The base case is an array with one element (which of course is sorted already), so merge sort immediately returns in this case Recursion step splits array into two approximately equal pieces, recursively sorts m, n merges two sorted arrays into one larger, sorted array

### Analysis of Binary Search algorithm and Selection Sort algorithm

Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to

### Merge Sort. 2004 Goodrich, Tamassia. Merge Sort 1

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets

### AP Computer Science Java Mr. Clausen Program 9A, 9B

AP Computer Science Java Mr. Clausen Program 9A, 9B PROGRAM 9A I m_sort_of_searching (20 points now, 60 points when all parts are finished) The purpose of this project is to set up a program that will

### Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

### CMPS 102 Solutions to Homework 1

CMPS 0 Solutions to Homework Lindsay Brown, lbrown@soe.ucsc.edu September 9, 005 Problem..- p. 3 For inputs of size n insertion sort runs in 8n steps, while merge sort runs in 64n lg n steps. For which

### 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

### Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

### Binary Heap Algorithms

CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

### Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Algorithms Efficiency of algorithms Computational resources: time and space Best, worst and average case performance How to compare algorithms: machine-independent measure of efficiency Growth rate Complexity

### Algorithms, Integers

CHAPTER 3 Algorithms, Integers 3.1. Algorithms Consider the following list of instructions to find the maximum of three numbers a, b, c: 1. Assign variable x the value of a. 2. If b > x then assign x the

### Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

1 Algorithm Analysis []: if-else statements, recursive algorithms COSC 011, Winter 004, Section N Instructor: N. Vlajic Algorithm Analysis for-loop Running Time The running time of a simple loop for (int

### UNIT 5C Merge Sort. Course Announcements

UNIT 5C Merge Sort 1 Course Announcements Exam rooms for Lecture 1, 2:30 3:20 Sections A, B, C, D at Rashid Sections E, F, G at Baker A51 (Giant Eagle Auditorium) Exam rooms for Lecture 2, 3:30 4:20 Sections

### Today s Outline. Exercise. Binary Search Analysis. Linear Search Analysis. Asymptotic Analysis. Analyzing Code. Announcements. Asymptotic Analysis

Today s Outline Announcements Assignment #1 due Thurs, Oct 7 at 11:45pm Asymptotic Analysis Asymptotic Analysis CSE 7 Data Structures & Algorithms Ruth Anderson Autumn 2010 Exercise Analyzing Code bool

### Format of Divide-and-Conquer algorithms:

CS 360: Data Structures and Algorithms Divide-and-Conquer (part 1) Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively

### Binary search algorithm

Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than

### DATA STRUCTURES USING C

DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

### CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential

### Zabin Visram Room CS115 CS126 Searching. Binary Search

Zabin Visram Room CS115 CS126 Searching Binary Search Binary Search Sequential search is not efficient for large lists as it searches half the list, on average Another search algorithm Binary search Very

### AP Computer Science AB Syllabus 1

AP Computer Science AB Syllabus 1 Course Resources Java Software Solutions for AP Computer Science, J. Lewis, W. Loftus, and C. Cocking, First Edition, 2004, Prentice Hall. Video: Sorting Out Sorting,

### Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

### VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANKULATHUR 603 203 DEPARTMENT OF COMPUTER APPLICATIONS QUESTION BANK IN REVISED BLOOM S TAXONOMY

ACADEMIC YEAR: 0 7 VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANKULATHUR 0 0 SEMESTER: ODD BRANCH: MCA YEAR: I SEMESTER: I SUBJECT CODE AND NAME: MC70 Problem Solving and Programming NAME OF THE FACULTY

### 6. Standard Algorithms

6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary

### Data Structures. Algorithm Performance and Big O Analysis

Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined

### A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

### Binary Search. Search for x in a sorted array A.

Divide and Conquer A general paradigm for algorithm design; inspired by emperors and colonizers. Three-step process: 1. Divide the problem into smaller problems. 2. Conquer by solving these problems. 3.

### 1 2-3 Trees: The Basics

CS10: Data Structures and Object-Oriented Design (Fall 2013) November 1, 2013: 2-3 Trees: Inserting and Deleting Scribes: CS 10 Teaching Team Lecture Summary In this class, we investigated 2-3 Trees in

### Mergesort and Quicksort

Mergesort Mergesort and Quicksort Basic plan: Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole. mergesort mergesort analysis quicksort quicksort analysis

### Computer Science 210: Data Structures. Searching

Computer Science 210: Data Structures Searching Searching Given a sequence of elements, and a target element, find whether the target occurs in the sequence Variations: find first occurence; find all occurences

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

### Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort).

Chapter 7: Quicksort Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort). The three steps of Quicksort are as follows:

### What Is Recursion? Recursion. Binary search example postponed to end of lecture

Recursion Binary search example postponed to end of lecture What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion

### Binary Trees and Huffman Encoding Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees Computer Science E119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary

### B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

### Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A Developed By Brian Weinfeld Course Description: AP Computer

### Data Structures and Data Manipulation

Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval

### 4. MATRICES Matrices

4. MATRICES 170 4. Matrices 4.1. Definitions. Definition 4.1.1. A matrix is a rectangular array of numbers. A matrix with m rows and n columns is said to have dimension m n and may be represented as follows:

### Vimal P.Parmar Research Scholar, Department of Computer Science Saurashtra University, Rajkot Gujarat, INDIA

Comparing Linear Search and Binary Search Algorithms to Search an Element from a Linear List Implemented through Static Array, Dynamic Array and Linked List Vimal P.Parmar Research Scholar, Department

### External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

External Sorting Chapter 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing

### Data Structures in the Java API

Data Structures in the Java API Vector From the java.util package. Vectors can resize themselves dynamically. Inserting elements into a Vector whose current size is less than its capacity is a relatively

### Chapter 7: Sequential Data Structures

Java by Definition Chapter 7: Sequential Data Structures Page 1 of 112 Chapter 7: Sequential Data Structures We have so far explored the basic features of the Java language, including an overview of objectoriented

### Curriculum Map. Discipline: Computer Science Course: C++

Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code

### GRID SEARCHING Novel way of Searching 2D Array

GRID SEARCHING Novel way of Searching 2D Array Rehan Guha Institute of Engineering & Management Kolkata, India Abstract: Linear/Sequential searching is the basic search algorithm used in data structures.

### Lecture Notes on Linear Search

Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is

### Divide-and-Conquer Algorithms Part Four

Divide-and-Conquer Algorithms Part Four Announcements Problem Set 2 due right now. Can submit by Monday at 2:15PM using one late period. Problem Set 3 out, due July 22. Play around with divide-and-conquer

### 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

### Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

### Systems Analysis and Design

Systems Analysis and Design Slides adapted from Jeffrey A. Hoffer, University of Dayton Joey F. George, Florida State University Joseph S. Valacich, Washington State University Modern Systems Analysis

### External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

External Sorting Chapter 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing

### In This Issue: Excel Sorting with Text and Numbers

In This Issue: Sorting with Text and Numbers Microsoft allows you to manipulate the data you have in your spreadsheet by using the sort and filter feature. Sorting is performed on a list that contains

### Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge

### CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)

### Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know

### , each of which contains a unique key value, say k i , R 2. such that k i equals K (or to determine that no such record exists in the collection).

The Search Problem 1 Suppose we have a collection of records, say R 1, R 2,, R N, each of which contains a unique key value, say k i. Given a particular key value, K, the search problem is to locate the

### Sample Questions Csci 1112 A. Bellaachia

Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k

### Heaps & Priority Queues in the C++ STL 2-3 Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

### Section IV.1: Recursive Algorithms and Recursion Trees

Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller

### Unit 1. 5. Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Unit 1 1. Write the following statements in C : [4] Print the address of a float variable P. Declare and initialize an array to four characters a,b,c,d. 2. Declare a pointer to a function f which accepts

### Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed

### The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

### In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Big O notation (with a capital letter O, not a zero), also called Landau's symbol, is a symbolism used in complexity theory, computer science, and mathematics to describe the asymptotic behavior of functions.

### 4.2 Sorting and Searching

Sequential Search: Java Implementation 4.2 Sorting and Searching Scan through array, looking for key. search hit: return array index search miss: return -1 public static int search(string key, String[]

### 10CS35: Data Structures Using C

CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a

### Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Searching (Unit 6) Outline Introduction Linear Search Ordered linear search Unordered linear search Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

### PES Institute of Technology-BSC QUESTION BANK

PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions

### 6 March 2007 1. Array Implementation of Binary Trees

Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of

### DATABASE DESIGN - 1DL400

DATABASE DESIGN - 1DL400 Spring 2015 A course on modern database systems!! http://www.it.uu.se/research/group/udbl/kurser/dbii_vt15/ Kjell Orsborn! Uppsala Database Laboratory! Department of Information

### Basic Programming and PC Skills: Basic Programming and PC Skills:

Texas University Interscholastic League Contest Event: Computer Science The contest challenges high school students to gain an understanding of the significance of computation as well as the details of

### Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

### Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Hash Tables Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited We ve considered several data structures that allow us to store and search for data

### Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number

### COMPUTER SCIENCE. Paper 1 (THEORY)

COMPUTER SCIENCE Paper 1 (THEORY) (Three hours) Maximum Marks: 70 (Candidates are allowed additional 15 minutes for only reading the paper. They must NOT start writing during this time) -----------------------------------------------------------------------------------------------------------------------

### Chapter 3. if 2 a i then location: = i. Page 40

Chapter 3 1. Describe an algorithm that takes a list of n integers a 1,a 2,,a n and finds the number of integers each greater than five in the list. Ans: procedure greaterthanfive(a 1,,a n : integers)

### CompSci-61B, Data Structures Final Exam

Your Name: CompSci-61B, Data Structures Final Exam Your 8-digit Student ID: Your CS61B Class Account Login: This is a final test for mastery of the material covered in our labs, lectures, and readings.

### Introduction to Parallel Programming and MapReduce

Introduction to Parallel Programming and MapReduce Audience and Pre-Requisites This tutorial covers the basics of parallel programming and the MapReduce programming model. The pre-requisites are significant

### Pseudo code Tutorial and Exercises Teacher s Version

Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy

### Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

Name : Jariya Phongsai Class : MAC 286. Data Structure Research Paper on Sorting Algorithms Prof. Lawrence Muller Date : October 26, 2009 Introduction In computer science, a ing algorithm is an efficient

### root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain

inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each

### Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

### BCS2B02: OOP Concepts and Data Structures Using C++

SECOND SEMESTER BCS2B02: OOP Concepts and Data Structures Using C++ Course Number: 10 Contact Hours per Week: 4 (2T + 2P) Number of Credits: 2 Number of Contact Hours: 30 Hrs. Course Evaluation: Internal

### AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm

AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm Time: Three Hours] [Maximum Marks: 60 Note Attempts all the questions. All carry equal marks Section A

### APP INVENTOR. Test Review

APP INVENTOR Test Review Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division

### Recursion vs. Iteration Eliminating Recursion

Recursion vs. Iteration Eliminating Recursion continued CS 311 Data Structures and Algorithms Lecture Slides Monday, February 16, 2009 Glenn G. Chappell Department of Computer Science University of Alaska

### Data Structure [Question Bank]

Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

### CS473 - Algorithms I

CS473 - Algorithms I Lecture 9 Sorting in Linear Time View in slide-show mode 1 How Fast Can We Sort? The algorithms we have seen so far: Based on comparison of elements We only care about the relative

### Searching Algorithms

Searching Algorithms The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g., int val = 7; Find the first index of the value in the

### JAVA NOTES DATA STRUCTURES AND ALGORITHMS

15 JAVA NOTES DATA STRUCTURES AND ALGORITHMS Terry Marris July 2001 3 LINEAR SEARCH 3.1 LEARNING OUTCOMES By the end of this lesson the student should be able to describe how a linear search algorithm

### MLR Institute of Technology

MLR Institute of Technology DUNDIGAL 500 043, HYDERABAD COMPUTER SCIENCE AND ENGINEERING Computer Programming Lab List of Experiments S.No. Program Category List of Programs 1 Operators a) Write a C program

### CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

### Persistent Binary Search Trees

Persistent Binary Search Trees Datastructures, UvA. May 30, 2008 0440949, Andreas van Cranenburgh Abstract A persistent binary tree allows access to all previous versions of the tree. This paper presents

### CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

### Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Motivation Suppose that we want a program that can read in a list of numbers and sort that list, or nd the largest value in that list. To be concrete about it, suppose we have 15 numbers to read in from

### Section of DBMS Selection & Evaluation Questionnaire

Section of DBMS Selection & Evaluation Questionnaire Whitemarsh Information Systems Corporation 2008 Althea Lane Bowie, Maryland 20716 Tele: 301-249-1142 Email: mmgorman@wiscorp.com Web: www.wiscorp.com

### Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

### Solving Problems Recursively

Solving Problems Recursively Recursion is an indispensable tool in a programmer s toolkit Allows many complex problems to be solved simply Elegance and understanding in code often leads to better programs:

### One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Arrays in Visual Basic 6 An array is a collection of simple variables of the same type to which the computer can efficiently assign a list of values. Array variables have the same kinds of names as simple

### Exponentials. 1. Motivating Example

1 Exponentials 1. Motivating Example Suppose the population of monkeys on an island increases by 6% annually. Beginning with 455 animals in 2008, estimate the population for 2011 and for 2025. Each year

### Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Sorting Let elem be a type with a operation, which is a total order A vector

### Data Structures Using Java

Data Structures Using Java D. S. Malik P. S. Nair THOMSON COURSE TECHNOLOGY Australia Canada Mexico Singapore Spain United Kingdom United States TABLE OF Contents PREFACE XXV 1.Software Engineering Principles