Sorting - Iterative Algorithms

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

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

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)

Binary Heap Algorithms

Data Structures. Algorithm Performance and Big O Analysis

Analysis of Binary Search algorithm and Selection Sort algorithm

Section IV.1: Recursive Algorithms and Recursion Trees

6. Standard Algorithms

CS473 - Algorithms I

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

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

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

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

Converting a Number from Decimal to Binary

Chapter 13: Query Processing. Basic Steps in Query Processing

AP Computer Science AB Syllabus 1

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

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Sample Questions Csci 1112 A. Bellaachia

CS 241 Data Organization Coding Standards

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

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

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

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

Data Structures and Algorithms Written Examination

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

4.2 Sorting and Searching

Pseudo code Tutorial and Exercises Teacher s Version

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

Analysis of a Search Algorithm

Exam study sheet for CS2711. List of topics

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

Algorithms. Margaret M. Fleck. 18 October 2010

Data Structures Fibonacci Heaps, Amortized Analysis

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

For example, we have seen that a list may be searched more efficiently if it is sorted.

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

Better Sorting Algorithms

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

6 March Array Implementation of Binary Trees

Binary Heaps. CSE 373 Data Structures

FOPR-I1O23 - Fundamentals of Programming

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

Why? A central concept in Computer Science. Algorithms are ubiquitous.

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

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

Data Structures. Level 6 C Module Descriptor

Data Structures. Topic #12

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

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

OPTIMAL BINARY SEARCH TREES

DATA STRUCTURES USING C

PES Institute of Technology-BSC QUESTION BANK

Memory Allocation Technique for Segregated Free List Based on Genetic Algorithm

Symbol Tables. Introduction

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

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

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Biostatistics 615/815

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

recursion, O(n), linked lists 6/14

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Computer Science 210: Data Structures. Searching

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Rethinking SIMD Vectorization for In-Memory Databases

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

COMPUTER SCIENCE (5651) Test at a Glance

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Cloud and Big Data Summer School, Stockholm, Aug Jeffrey D. Ullman

Lecture Notes on Linear Search

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

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Persistent Binary Search Trees

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:

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

Unit 6. Loop statements

Introduction to Data Structures

Data Structures and Data Manipulation

CUDA Programming. Week 4. Shared memory and register

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

CS/COE

CIS 631 Database Management Systems Sample Final Exam

Loop Invariants and Binary Search

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Analysis of Computer Algorithms. Algorithm. Algorithm, Data Structure, Program

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

DESIGN AND ANALYSIS OF ALGORITHMS

Figure 1: Graphical example of a mergesort 1.

Display Format To change the exponential display format, press the [MODE] key 3 times.

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

String Search. Brute force Rabin-Karp Knuth-Morris-Pratt Right-Left scan

Reshaping & Combining Tables Unit of analysis Combining. Assignment 4. Assignment 4 continued PHPM 672/677 2/21/2016. Kum 1

BM307 File Organization

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Using SQL Server Management Studio

10CS35: Data Structures Using C

50 Computer Science MI-SG-FLD050-02

Transcription:

Sorting - Iterative Algorithms Problem Definition Input: A sequence of n elements <a 1, a 2,, a n > Output: A permutation <a 1, a 2,, a n > of such elements, so that a 1 a 2 a n 2 1

Types of Ordering Internal Ordering All the elements to be ordered are in main memory Direct access to all elements External Ordering Elements cannot be loaded all in memory at the same time It is necessary to act on elements stored on a file Usually, sequential access 3 Practical observations Elements to be ordered are usually structures (struct) made of many variables (fields) The key of such structure is usually one field (or a value calculated from one or more fields) Remaining fields are additional data but useless for ordering Ordering is made for increasing values of the key 4 2

Example struct student { int id; ; char surname[30] ; char name[30] ; int grade; struct student class[100] ; 5 Example struct student { ; int id; char surname[30] ; char name[30] ; int grade; Ordering by grade( repeated values) Ordering by id struct student class[100] ; Ordering by name and surname (key = concatenation name and surname) 6 3

Stability A sorting algorithm is called stable whenever, even if there are elements with the same value of the key, in the resulting sequence such elements appear in the same order in which they appeared in the initial sequence. 7 Simple Assumption During the study of sorting algorithms there are usually arrays of n integer values: int A[n] ; 8 4

Algorithms There are many sorting algorithms with different complexity: O(n 2 ): simple, iterative Insertion sort, Selection sort, Bubble sort, O(n): only applicable in particular cases Counting sort, Radix sort, Bin (or Bucket) sort, O(n log n): more complex, recursive Merge sort, Quicksort, Heapsort 9 Insertion sort Already sorted v[j] Not considered yet 2 3 6 12 16 21 8 Move forward all the elements so that v[ I ] > v[ j ] 2 3 6 8 12 16 21 2 3 6 8 12 16 21 5 10 5

Pseudo-code Insert A[ j ] in ordered sequence A [1.. j-1] i > 0 AND A[i ] > key 11 Implementation in C void InsertionSort(int A[], int n) { int i, j, key ; for(j=1; j<n; j++) { key = A[j] ; i = j - 1 ; while ( i >= 0 && A[i]>key ) { A[i+1] = A[i] ; i-- ; A[i+1] = key ; 12 6

From pseudo-code to C Note well: In C, array indexes are from 0 to n-1, while pseudo-code use ranges from 1 to n. Indentation of code is useful but remember braces to identify blocks { 13 Complexity Number of comparisons: C min = n-1 C avg = ¼(n 2 +n-2) C max = ½(n 2 +n)-1 Number of data-copies M min = 2(n-1) M avg = ¼(n 2 +9n-10) M max = ½(n 2 +3n-4) Best case: array already ordered Worst case: array ordered inversely C = O(n 2 ), M = O(n 2 ) T(n) = O(n 2 ) T(n) non è (n 2 ) T worst case (n) = (n 2 ) 14 7

Other quadratic algorithms Average Insertion Sort Selection Sort Bubble Sort 15 Execution Time (ms) Direct Insertion Binary Insertion Direct Selection Bubble sort Bubble sort with change notification Shaker sort Shell sort Heap sort Quick sort Merge Ordered Random Inversely Ordered n = 256 512 256 512 256 512 16 8

Impact of data n = 256 2 byte 16 byte 2 byte 16 byte 2 byte 16 byte 17 Counting sort It cannot be applied in general, as it is based on this hypothesis: The n elements to be ordered are integer numbers between 1 and k, with k integer. With such hypothesis, if k = O(n), then the algorithm s complexity is just O(n). 18 9

Basic Idea Find out, for each element x, how many elements of the array are less than x. Such information allows to put x directly in the final position in the array 19 Data Structure Three arrays are needed: Initial array: A[1..n] Final array: B[1..n] Temporary Array: C[1..k] Array C keeps track of number of elements of A having a certain value: C[i] is the number of elements of A equals to i. Sum of the first i elements of C defines the number of elements of A whose values is <= i 20 10

Pseudo-code 21 Analysis For each j, C[ A[j] ] represents the number of elements less than or equals to A[j], and then it is the final position of A[j] in B: B[ C[ A[j] ] ] = A[j] The correction C[ A[j] ] C[ A[j] ] 1 is needed to handle duplicate elements. 22 11

Example (n=8, k=6) A C C 3 6 4 1 3 4 1 4 2 0 2 3 0 1 2 2 4 7 7 8 B 4 C 2 2 4 6 7 8 B 1 4 C 1 2 4 6 7 8 23 Example (2) A 3 6 4 1 3 4 1 4 B 4 C 2 2 4 6 7 8 B 1 4 C 1 2 4 6 7 8 B 1 4 4 C 1 2 4 5 7 8 B 1 3 4 4 C 1 2 3 5 7 8 B 1 1 3 4 4 C 0 2 3 5 7 8 j=8 j=7 j=6 j=5 j=4 B 1 1 3 4 4 4 C 0 2 3 4 7 8 j=3 B 1 1 3 4 4 4 6 C 0 2 3 4 7 7 j=2 B 1 1 3 3 4 4 4 6 C 0 2 2 4 7 7 j=1 24 12

Complexity 1-2: Initialization of C: O(k) 3-4: Calculate C: O(n) 6-7: Sum in C: O(k) 9-11: Copy in B: O(n) Total complexity is O(n+k). Algorithm is useful only when k=o(n), because the resulting complexity is O(n). 25 Note The condition of applicability of the algorithm can be extended in this way: The key field of n elements to be ordered has a limited number of possible values k. 26 13

Bubble Sort In each cycle compare every couple of consecutive elements and if they are not ordered, then swap (exchange) them. Repeat this process N times and all the elements will be ordered Complexity is O(n 2 ) Optimization: if during last cycle there are no swaps, then the elements are already sorted 27 Bubble sort in C void BubbleSort(int A[], int n) { int i, j, t; for(i=1; i<n-1; i++) { for(j=1; j<n-1; j++) { if ( A[j]>A[j+1] ) { t = A[j] ; A[j] = A[j+1]; A[j+1] = t; 28 14

Bubble sort (optimized) in C void BubbleSort2(int A[], int n) { int i, j, t, repeat = 1; While (repeat) { repeat=0; /*if no swaps remains 0-> exit while*/ for(j=1; j<n-1; j++) { if ( A[j]>A[j+1] ) { t = A[j] ; /* swap elements*/ A[j] = A[j+1]; A[j+1] = t; repeat=1; 29 15