COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

Slides:



Advertisements
Similar presentations
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Sorting Leena A PGT Comp SC KV No:2 Jalahalli. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores.
Match-and-Stop Search Will find FIRST match Use Boolean variable to denote whether a match has been found or not Found initially False If a match is found,
Sorting1 Sorting Order in the court!. sorting2 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%
Insertion Sorting Lecture 21. Insertion Sort Start from element 2 of list location 1 –In first iteration: Compare element 1 with all of its elements to.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
C++ Plus Data Structures
Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
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.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
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.
CSCI 51 Introduction to Programming March 12, 2009.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
3 – SIMPLE SORTING ALGORITHMS
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Lists in Python Selection Sort Algorithm. Sorting A very common activity for computers Not just in business, sorting is used in databases, graphics, simulations,
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Sorting Sorting takes an unordered array and makes it an ordered one
1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
CSCI 51 Introduction to Programming March 10, 2009.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
Alg2_1c Extra Material for Alg2_1
Selection Sort – an array sorting algorithm
Sorting Algorithms Ellysa N. Kosinaya.
Sorting Example Bubble Sort
Introduction to Sorting Algorithms
Principles of Computing – UFCFA3-30-1
Presentation transcript:

COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 132 Sorting Technique to arrange a set of items in sequence Ascending Descending Sorting Applications To prepare an attendance sheet of student  Either sorted by name or ID for easy checking

COMP102 Lab Selection Sort Selection sort performs sorting by repeatedly putting the largest element in the unsorted portion of the array to the end of this unsorted portion Until the whole array is sorted Algorithm 1. Define the entire array as the unsorted portion of the array 2. While the unsorted portion of the array has more than one element  Find its largest element  Swap with last element  Reduce the size of the unsorted portion of the array by 1

COMP102 Lab Selection Sort – Example Before sorting After pass After pass After pass After pass 1

COMP102 Lab Selection Sort – Example E.g. // Sort array of integers in ascending order void select(int data[size], int size) { int temp; // for swap int max_index; // index of max value for (int rightmost=size-1; rightmost>0; rightmost--) { // find the largest item max_index = 0; for(int current=1; current<=rightmost; current++) if (data[current] > data[max_index]) max_index = current; // swap with last item if necessary if(data[max_index] > data[rightmost]) { temp = data[max_index]; data[max_index] = data[rightmost]; data[rightmost] = temp; }

COMP102 Lab Bubble Sort Bubble sort examines the array from start to finish Comparing elements as it goes Any time it finds a larger element before a smaller element It swaps the two  The larger elements are passed towards the end Then it repeats the process for the unsorted portion of the array Until the whole array is sorted

COMP102 Lab Bubble Sort Algorithm Define the entire array as the unsorted portion of the array While the unsorted portion of the array has more than one element 1. For every element in the unsorted portion Swap with the next neighbor if it is larger than the neighbor 2. Reduce the size of the unsorted portion of the array by 1

8 2. Bubble Sort – Example Before sorting outer=7, inner= outer=7, inner= outer=7, inner= outer=7, inner= outer=7, inner= outer=7, inner= outer=6, inner= outer=6, inner= outer=6, inner= outer=6, inner= outer=5, inner= outer=5, inner= outer=5, inner= outer=4, inner= … outer=1, inner=

COMP102 Lab Bubble Sort – Example E.g. // Sort an array of integers in ascending order void bubble(int data[], int size) { int temp; for (int outer=size-1; outer>0; outer--) { for (int inner=0; inner < outer; inner++) { // traverse the nested loops // if the current element is greater // swap current element with next if ( data[inner] > data[inner+1] ) { temp = data[inner]; data[inner] = data[inner+1]; data[inner+1] = temp; } } // inner for loop } // outer for loop }

COMP102 Lab 1310 SUMMARY By the end of this lab, you should be able to: Sorting an unordered array with  Selection sort  Bubble sort