Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Similar documents
SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Data Structures and Algorithms (DSA) Course 12 Trees & sort. Iulian Năstac

Sorting Pearson Education, Inc. All rights reserved.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Sorting. Introduction. Classification

Chapter 8 Algorithms 1

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Sorting Algorithms. + Analysis of the Sorting Algorithms

Algorithms Interview Questions

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Overview of Sorting Algorithms

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Cpt S 122 Data Structures. Sorting

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

O(n): printing a list of n items to the screen, looking at each item once.

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Faster Sorting Methods

Searching, Sorting. Arizona State University 1

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Searching and Sorting

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Module 2: Classical Algorithm Design Techniques

UNIT 7. SEARCH, SORT AND MERGE

Data Structures and Algorithms (DSA) Course 13 Algorithms

Advanced Algorithms, Third Class, Computer Science Department, CSW,

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Sorting & Searching. Hours: 10. Marks: 16

Lecture 9: Sorting Algorithms

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

Lecture 6 Sorting and Searching

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

CS240 Fall Mike Lam, Professor. Quick Sort

Sorting: Overview/Questions

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Sorting and Searching

Searching & Sorting in Java Bubble Sort

Data Structures and Algorithms Sorting

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

C/C++ Programming Lecture 18 Name:

Search,Sort,Recursion

Mergesort again. 1. Split the list into two equal parts

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Data Structures and Algorithms

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

SORTING AND SEARCHING

Chapter 7 Sorting. Terminology. Selection Sort

Sorting (Weiss chapter )

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

Programming II (CS300)

Sorting is a problem for which we can prove a non-trivial lower bound.

COS 226 Midterm Exam, Spring 2009

Algorithm Analysis. Performance Factors

CSE 373: Data Structures and Algorithms

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

Binary Search APRIL 25 TH, 2014

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

CISC 1100: Structures of Computer Science

PERFORMANCE OF VARIOUS SORTING AND SEARCHING ALGORITHMS Aarushi Madan Aarusi Tuteja Bharti

lecture notes September 2, How to sort?

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Sorting. Weiss chapter , 8.6

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Programming II (CS300)

Sorting is ordering a list of objects. Here are some sorting algorithms

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

II (Sorting and) Order Statistics

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Binary Search Trees Treesort

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Lecture 7: Searching and Sorting Algorithms

Mergesort again. 1. Split the list into two equal parts

SORTING AND SELECTION

Lecture Notes on Quicksort

Sorting. Order in the court! sorting 1

Chapter 10 Sorting and Searching Algorithms

Computer Science 1000: Part #2. Algorithms

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Visualizing Data Structures. Dan Petrisko

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

Transcription:

Computer Science 4U Unit 1 Programming Concepts and Skills Algorithms

Algorithm In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as randomized algorithms, incorporate random input.

Computer Algarithms A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows. This diagram representation illustrates a solution to a given problem. Process operations are represented in these boxes, and arrows.

Here s a flowchart algorithm to find the greatest common divisor between two given numbers. For a list of symbols, go here

SEARCH

Linear Search In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. Linear search is the simplest search algorithm; it is a special case of brute-force search.

Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list elements are equally likely to be searched for. Therefore, if the list has more than a few elements, other methods (such as binary search or hashing) will be faster, but they also impose additional requirements.

Binary Search Binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value. For binary search, the array should be arranged in ascending or descending order. In each step, the algorithm compares the search key value with the key value of the middle element of the array.

If the keys match, then a matching element has been found and its index, or position, is returned. Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right. If the remaining array to be searched is empty, then the key cannot be found in the array and a special "not found" indication is returned.

A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm. Example: The list to be searched: L = 1 3 4 6 8 9 1 The value to be found: X = 4. Compare X to 6. X is smaller. Repeat with L = 1 3 4. Compare X to 3. X is bigger. Repeat with L = 4. Compare X to 4. They are equal. We're done, we found X.

SORTING INFORMATION

Bubble Sort Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list.

Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

Insertion Sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

Simple implementation Efficient for (quite) small data sets Adaptive (i.e., efficient) for data sets that are already substantially sorted More efficient in practice than most other simple quadratic algorithms such as selection sort or bubble sort Stable; i.e., does not change the relative order of elements with equal keys In-place; i.e., only requires a constant amount of additional memory space Online; i.e., can sort a list as it receives it When people manually sort something (for example, a deck of playing cards), most use a method that is similar to insertion sort.

Selection Sort Selection sort is a sorting algorithm, specifically an in-place comparison sort. It is inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Here is an example of this sort algorithm sorting five elements: 64 25 12 22 11 // this is the initial, starting state of the array 11 25 12 22 64 // sorted sublist = {11} 11 12 25 22 64 // sorted sublist = {11, 12} 11 12 22 25 64 // sorted sublist = {11, 12, 22} 11 12 22 25 64 // sorted sublist = {11, 12, 22, 25} 11 12 22 25 64 // sorted sublist = {11, 12, 22, 25, 64}

Now create flowchart algorithms for the following Linear search for a letter (eg: a ) Binary search for a word (eg: the ) A bubble sort An insertion sort A selection sort For all examples, use either an array or a list as your source of data.

You may draw the flowcharts by hand if you want. (give hard copy to Mr. Fontaine in class) For electronic file Save your work as john_s_1_7_algor (where john_s is replaced by your name) under the appropriate folder for your class in the DropOff folder on the X: drive