Data Structures and Organization (p.2 – Arrays)

Slides:



Advertisements
Similar presentations
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Advertisements

CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
By D.Kumaragurubaran Adishesh Pant
CSC220 Data Structure Winter
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Sorting. Background As soon as you create a significant database, you’ll probably think of reasons to sort it in various ways. You need to arrange names.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 19: Searching and Sorting Algorithms
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.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
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.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
OCR A Level F453: Data structures and data manipulation Data structures and data manipulation a. explain how static data structures may be.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
May 17th – Comparison Sorts
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Lecture 14 Searching and Sorting Richard Gesick.
Search Recall the employee database used in class examples: employeeRecordT workforce[5] = Given "Vance, Emelline", how can we find her salary? Name.
Simple Sorting Algorithms
Ordered Array.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Design and Analysis of Algorithms
Data Structures and Algorithms
Topic 14 Searching and Simple Sorts
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.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Bubble Sort The basics of a popular sorting algorithm.
CSc 110, Spring 2017 Lecture 39: searching.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Chapter 8 Search and Sort
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Sort Techniques.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
A Robust Data Structure
Searching and Sorting Topics Sequential Search on an Unordered File
24 Searching and Sorting.
Topic 14 Searching and Simple Sorts
Principles of Computing – UFCFA3-30-1
Sorting "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." -The Sorting Hat, Harry Potter.
Topic 24 sorting and searching arrays
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Structures & Algorithms
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
B-Trees.
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Data Structures and Organization (p.2 – Arrays) Yevhen Berkunskyi, Computer Science dept., NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua

What arrays are? The array is the most commonly used data storage structure; it’s built into most programming languages. An array is a data structure represented as a group of cells of the same type, with one common name. Arrays are used to process a large number of similar data.

Class as Data Structure Unit Suppose you’re coaching kids-league basketball, and you want to keep track of which players are present at the practice field What you need is an attendance-monitoring program for your laptop—a program that maintains a database of the players who have shown up for practice. You can use a simple data structure to hold this data. What kind of data we’ll use for player? Let it be Player class:

The Player class public class Player { private int num; private String name; public Player(int num, String name) { this.num = num; this.name = name; } @Override public String toString() { return "{" + num + ", " + name + "}"; ...

Actions Insert a player into the data structure when the player arrives at the field Check to see whether a particular player is present, by searching for the player’s number in the structure Delete a player from the data structure when that player goes home These three operations: insertion, searching, and deletion — will be the fundamental ones in most of the data storage structures

Example

Ordered Array Imagine 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. Such an array is called an ordered array. 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.

Binary Searching Why would we want to arrange data in order? One advantage is that we can speed up search times dramatically using a binary search. The Guess-a-Number Game Binary search uses the same approach you did as a kid (if you were smart) to guess a number in the well-known children’s guessing game. In this game, a friend asks you to guess a number she’s thinking of between 1 and 100.

Binary Searching

Example

Simple Sorting Compare two items. Swap two items, or copy one item. The three algorithms in this presentation all involve two steps, executed over and over until the data is sorted: Compare two items. Swap two items, or copy one item. However, each algorithm handles the details in a different way Bubble Sort Selection Sort Insertion Sort

Bubble Sort The bubble sort is notoriously slow, but it’s conceptually the simplest of the sorting algorithms and for that reason is a good beginning for our exploration of sorting techniques. Here are the rules you’re following: 1. Compare two elements. 2. If the one on the left is greater, swap them. 3. Move one position right.

Bubble Sort Compare two elements. If the one on the left is greater, swap them. Move one position right. Efficiency of the Bubble Sort (N–1) + (N–2) + (N–3) + ... + 1 = N*(N–1)/2

Bubble Sort

Selection Sort A Brief Description What’s involved in the selection sort is making a pass through all the elements and picking (or selecting, hence the name of the sort) the smallest one. This smallest element is then swapped with the element on the left end of the line, at position 0. Now the leftmost element is sorted and won’t need to be moved again. Notice that in this algorithm the sorted elements accumulate on the left (lower indices), whereas in the bubble sort they accumulated on the right.

Selection Sort Efficiency of the Selection Sort The selection sort performs the same number of comparisons as the bubble sort: N*(N-1)/2

Selection Sort

Insertion Sort At this point there’s an imaginary marker somewhere in the middle of the line. The elements to the left of this marker are partially sorted. This means that they are sorted among themselves; each one is greater than the element from left. However, the elements aren’t necessarily in their final positions because they may still need to be moved when previously unsorted elements are inserted between them.

Insertion Sort

QuickSort

Questions?

Data Structures and Organization (p.2 – Arrays) Yevhen Berkunskyi, Computer Science dept., NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua