1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 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
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 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.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Searching and Sorting Arrays
 2007 Pearson Education, Inc. All rights reserved C 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.
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.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Computer Science Searching & Sorting.
Searching and Sorting Chapter Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
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.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
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.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
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 and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
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.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
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.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
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.
Objectives You should be able to describe: One-Dimensional Arrays
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Arrays … The Sequel Applications and Extensions
Introduction to Search Algorithms
MSIS 655 Advanced Business Applications Programming
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
CHAPTER 9 SORTING & SEARCHING.
Presentation transcript:

1 Two-Dimensional Arrays

2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3] [4] = {8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10};

3 Memory layout of two-dimensional arrays Users Perception or interpretation Actual memory layout:

4 Initialization: #define NUMROWS 3 #define NUMCOLS 4 int val[NUMROWS][NUMCOLS] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; The inner braces can be omitted: int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10}; Initialization is done in row order Two-Dimensional Arrays

5 #define NUMROWS 3 #define NUMCOLS 4 int val [NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};

6 Initializing Two-Dimensional Arrays Elements Can be initialized during declaration Values are grouped by rows Example: int scores[3][3]= {{3,5,7}, {2,4,6}, {11,9,13}};

7 Reading in the two-dimensional array elements Read values from the keyboard or a file Can be done using a loop (for loop) Example: int x[3][3]; int i, j; for (i = 0; i < 3; i++) { printf(“Enter the values in row %d: ”, i+1); for (j = 0; j < 3; j++) scanf (“%d”, &x[i][j]); }

8 Displaying in the two- dimensional array elements Example: int x[3][3]= {{3,5,7},{2,4,6},{11,9,13}}; int i, j; for (i = 0; i < 3; i++) { printf("Displaying values for row %d: \n", i+1); for (j = 0; j < 3; j++) printf("%d ", x[i][j]); printf("\n"); }

9 Sorting Array Values

10 Sorting Array Values To arrange the values in array in certain logical order. Various sorting algorithms  Selection sort  Bubble sort  Insertion sort  Quick sort

11 The Selection Sort algorithm List is divided into two sub lists, sorted and unsorted, which is divided by imaginary wall. Uses n-1 passes (where n is the number of elements) For each pass, the smallest element selected and placed to its correct position.

12 How selection sort works 1.Find the minimum value in the list 2.Swap it with the value in the first position 3.Repeat the steps above for remainder of the list (starting at the second position) Example of this sort algorithm sorting a total of six elements:

13 Code for a simple selection sort for(x=0; x<n; x++) {index_of_min = x; for(int y=x; y<n; y++) { if(array[index_of_min]<array[y]) index_of_min = y; temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; } (swapping values) *Refer to note : Exchanging values in an array

14 Bubble Sort Most popular, easiest to understand Values are swapped until list is sorted “Bubble”-like movement of lowest value to top of list *Please note that all this results after only one iteration. This loop will repeat itself until all values are sorted in increasing order. The order is ok (7 & 9), no need to swap Swap 7&4 Swap 7&1 Swap 9&2

15 The Bubble Sort algorithm Uses n passes (where n is the number of elements) For each pass, an element is compared with the next element down the list and swapped if not in correct order. for (i = (array_size - 1); i >= 0; i--) {for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) {temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; }

16 The Insertion Sort algorithm Uses n-1 passes For each pass, the smallest element selected and placed to its correct position. for (i=1; i<n; i++) {j=i; 2 t=a[j]; while (j>0 && a[j-1]>t) { a[j]=a[j-1]; j--; } a[j]=t; }

17 Insertion Sort Algorithm Here is an example: (0) (2) (2) (2) (4) (1) (6) For each iteration, the number of positions the inserted element has moved is shown in brackets. On the left side the sorted part of the sequence is shown in red.

18 Searching Array Values

19 Searching Arrays The process used to find the location of a target among a list of objects In the case of an array, searching means that given a value, we want to find the location (index) of the first element in the array that contains the value. Types of Search  Sequential or Linear Search  Binary Search

20 Sequential / Linear Search Easy, but inefficient Start at the beginning and stop when a match is found Does not require arrays to be sorted

21 Pseudocode for linear search technique For each item in the list.  Check to see if the item you're looking for matches the item in the list. If it matches, return the location where you found it (the index). If it does not match, continue searching until you reach the end of the list. –If we get here, we know the item does not exist in the list.

22 Sequential / Linear Search (Example)

23 Binary Search For sorted list or arrays only Compares the search value with value in the middle. Splits the list into two: works with half the list each time until a match is found Faster