Ordered Array.

Slides:



Advertisements
Similar presentations
Data Structures Arrays.
Advertisements

Overview of Data Structures and Algorithms
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Simple Sorting Algorithms
Searching/Sorting Introduction to Computing Science and Programming I.
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.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Tree.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
CSC 211 Data Structures Lecture 13
3 – SIMPLE SORTING ALGORITHMS
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
2 - Arrays Introducing Arrays Declaring Array Variables, Creating Arrays, and Initializing Arrays Ordered and Unordered Arrays Common Operations: Insertion,
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Maitrayee Mukerji. INPUT MEMORY PROCESS OUTPUT DATA INFO.
CS522 Advanced database Systems Huiping Guo Department of Computer Science California State University, Los Angeles 3. Overview of data storage and indexing.
Searching and Sorting Searching algorithms with simple arrays
16 Searching and Sorting.
Chapter 5 Ordered List.
INLS 623– Database Systems II– File Structures, Indexing, and Hashing
Indexing Structures for Files and Physical Database Design
Record Storage, File Organization, and Indexes
Chapter 5 Ordered List.
2 - Arrays Introducing Arrays
Simple Sorting Algorithms
Data Structures Interview / VIVA Questions and Answers
Introduction to Algorithms
Sorting by Tammy Bailey
Tonga Institute of Higher Education
Array.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Arrays … The Sequel Applications and Extensions
Chapter 5 Ordered List.
Data Structures and Organization (p.2 – Arrays)
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Disk storage Index structures for files
Searching and Sorting Topics Sequential Search on an Unordered File
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Unit-2 Divide and Conquer
Chapter 8 Search and Sort
B- Trees D. Frey with apologies to Tom Anastasio
Searching and Sorting Topics Sequential Search on an Unordered File
Manipulating lists of data
B- Trees D. Frey with apologies to Tom Anastasio
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Chapter 5 Ordered List.
A Robust Data Structure
Searching and Sorting Topics Sequential Search on an Unordered File
Searching CLRS, Sections 9.1 – 9.3.
Manipulating lists of data
24 Searching and Sorting.
B- Trees D. Frey with apologies to Tom Anastasio
Data Structures Sorted Arrays
Topic 24 sorting and searching arrays
Simple Sorting Algorithms
Data Structures Introduction
Simple Sorting Algorithms
Indexing, Access and Database System Architecture
Simple Sorting Algorithms
Searching.
Presentation transcript:

Ordered Array

Definition An array in which the data items are arranged in order of ascending key values—that is, with the smallest value at index 0, and each cell holding a value larger than the cell below. When we insert an item into this array, the correct location must be found for the insertion: just above a smaller value and just below a larger one. Then all the larger values must be moved up to make room. Why would we want to arrange data in order? One advantage is that we can speed up search times dramatically using a binary search. In the ordered array, if we’ve choose not to allow duplicates, this decision speeds up searching somewhat but slows down insertion.

Linear Search Linear searches operate in much the same way as the searches in the unordered array in the Array applet: The red arrow steps along, looking for a match. The difference is that in the ordered array, the search quits if an item with a larger key is found. Deletion works much the same as it did in the Array applet, shifting items with higher index numbers down to fill in the hole left by the deletion. In the ordered array, however, the deletion algorithm can quit partway through if it doesn’t find the item, just as the search routine can.

Binary Search The payoff for using an ordered array comes when we use a binary search. This kind of search is much faster than a linear search, especially for large arrays.

Binary Search (cont’) If you used a linear search, guessing first 1, then 2, then 3, and so on, finding the number would take you, on the average, 50 guesses. In a binary search each guess divides the range of possible values in half, so the number of guesses required is far fewer.

Binary Search with the find() Method public int find(long searchKey) { int lowerBound = 0; int upperBound = nElems-1; int curIn; while(true) curIn = (lowerBound + upperBound ) / 2; if(a[curIn]==searchKey) return curIn; // found it else if(lowerBound > upperBound) return nElems; // can’t find it else // divide range if(a[curIn] < searchKey) lowerBound = curIn + 1; // it’s in upper half else upperBound = curIn - 1; // it’s in lower half } // end else divide range } // end while } // end find()

Binary Search with the find() Method (cont’)

Advantages of Ordered Arrays What have we gained by using an ordered array? Search times are much faster than in an unordered array. Insertion takes longer because all the data items with a higher key value must be moved up to make room. Deletions are slow in both ordered and unordered arrays because items must be moved down to fill the hole left by the deleted item. Ordered arrays are therefore useful in situations in which searches are frequent, but insertions and deletions are not. An ordered array might be appropriate for a database of company employees, for example. Hiring new employees and laying off existing ones would probably be infrequent occurrences compared with accessing an existing employee’s record for information, or updating it to reflect changes in salary, address, and so on. A retail store inventory, on the other hand, would not be a good candidate for an ordered array because the frequent insertions and deletions, as items arrived in the store and were sold, would run slowly.

Storing Objects In the Java examples so far, we’ve stored primitive variables of type long in our data structures. Storing such variables simplifies the program examples, but it’s not representative of how you use data storage structures in the real world. Usually, the data items (records) you want to store are combinations of many fields. For a personnel record, you would store last name, first name, age, Social Security number, and so forth. For a stamp collection, you would store the name of the country that issued the stamp, its catalog number, condition, current value, and so on.

Comparing the Method

Summary Arrays in Java are objects, created with the new operator. Unordered arrays offer fast insertion but slow searching and deletion. Wrapping an array in a class protects the array from being inadvertently altered. A class interface is composed of the methods (and occasionally fields) that the class user can access. A class interface can be designed to make things simple for the class user. A binary search can be applied to an ordered array. Linear searches require time proportional to the number of items in an array. Binary searches require time proportional to the logarithm of the number of items.