Chapter 13 Lists. List  List  A variable-length, linear collection of homogeneous components  Example  StudentRec Students[100];  A list of 100 students.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
HST 952 Computing for Biomedical Scientists Lecture 9.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Simple Sorting Algorithms
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Computer Programming Sorting and Sorting Algorithms 1.
Searching Arrays Linear search Binary search small arrays
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.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
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.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Array operations II manipulating arrays and measuring performance.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Problem Solving and Algorithms
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Applications of Arrays (Searching and Sorting) and Strings
File Processing - Indexing MVNC1 Indexing Jim Skon.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
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.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
3 – SIMPLE SORTING ALGORITHMS
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Copyright © Curt Hill Sorting Ordering an array.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Algorithms
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
Recitation 13 Searching and Sorting.
Sorting Data are arranged according to their values.
Sorting Data are arranged according to their values.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Searching and Sorting Arrays
Priority Queue and Heap
Insertion Sort Array index Value Insertion sort.
Presentation transcript:

Chapter 13 Lists

List  List  A variable-length, linear collection of homogeneous components  Example  StudentRec Students[100];  A list of 100 students structures

Common Operations  On almost all data structures, there are 4 common activities 1.Insertion 2.Deletion 3.Searching 4.Sorting  Sorting can be handled at insertion or deferred until a later time.

Insertion  Insertion into an unordered list simply requires placing the next record at the end of the list.  Insertion into an order list requires finding its location and making a space to place the item.

Deletion  You must find the record you desire and remove it from the list. In an array based list, this will require some shifting.  If order is unimportant, you can simply swap it with the last record and reduce the number of records by one  If order is important, you must shift all the records after it down.

Searching  A common activity on data structures is searching.  Searching an array based list is simple for (index=0; index < length && !found; index++ ) if ( item == data[index] ) found = true; //index now contains the location of the item for which you were searching

Searching an Ordered List  If the list is ordered, there is a better way to search.  A Binary Search can be performed.  This will greatly speed up your search.  The idea is you look at the middle item.  If it is bigger than what you are looking for, the item lies to the left of the middle item.  If it is smaller than what you are looking for, the item lies to the right of the middle item.  Choose the appropriate side, and go again.

Sorting  Sorting can be done at the time of insertion, an insertion sort.  Or after insertion.  There are reasons to do both.  For our purposes, we will sort after.  Keeping an array in sorted order is not fun or worth the effort.

Selection Sort for (passCount = 0; passCount<length-1;passCount++ ) { minIndx = passCount; for (srchIndx=passCount+1;srchIndx<length;srchIndx++) if ( data[srchIndx] < data[minIndx] ) minIndx = srchIndx; temp = data[minIndx]; data[minIndx] = data[passCount]; data[passCount] = temp; }}

Bubble Sort  There is another sort known as bubble sort.  It is an awful sort.  It is essentially the same sort as selection sort, except that is makes the swap anytime that it finds an element is in the wrong spot.

Binary Search int first = 0; int last = length – 1; int middle; found = false; while ( last >= first && !found ) { middle = ( first + last ) / 2; if ( item < data[middle] ) last = middle – 1;

else if ( item > data[middle] ) first = middle + 1; else if ( item == data[middle] ) found = true; } if ( found ) position = middle;

How does this apply to me?  For your last project you will create a list of data items.  You will use a struct to hold the data and an array for the list.  The list will be unordered, but it will support sorting through the use of selection sort.  That will require modifying what has been show slightly to work with the struct.