11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
One Dimensional Arrays
Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 9: Advanced Array Concepts
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
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.
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.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
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.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Objectives You should be able to describe: One-Dimensional Arrays
Chapter 9: Sorting and Searching Arrays
An Introduction to Programming with C++ Sixth Edition
Arrays 2.
Program to search an element of array using linear search.
Sorting Data are arranged according to their values.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Sorting Data are arranged according to their values.
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending order. A sorting list is called an ordered list. arr[0]<= arr[1] <= ….,<= arr[n-2]<=arr[n-1]

Bubble Sort Comparing two consecutive array elements and swapping them if the first is greater than the second. At the end of the first pass,we have succeeded in pushing the largest value in the array to its end. Fig 11.9 A C function that sorts an array using bubble sort. typedef int list_item_type;

Selecting Sort Of selecting the largest array element and swapping it with the last array element. Figure A C function that sorts an array using selection sort

Insertion Sort Of inserting a new item into a list of ordered items. Figure A C function that sorts an array using insertion sort.

Constructing a Sort Library Figure the header file sort.h for the programmer-defined sort library. #include “sort.h”.

11.6 SEARCHING ARRAYS We search a list stored in an array to determine whether it contains an element that matching a given

Sequential Search We access list elements, starting with the first,and compare each element with the search key. If we find a match, the search is successful. If we access and compare the last list element and still have no match, then the search is unsuccessful.

Improve the efficiency of this algorithm by running it on ordered list. Algorithm terminates if the search key value turns out to be less than an array element.

Binary Search Compares the search key value with the value of the list element that is midway in the list. Figure A C function that searches an array using binary search

Figure Comparing efficiencies of sequential and binary search List_size sequential search binary search ,000,000 1,000, ,000,000,000 1,000,000,000 30

Constructing Search Library #include “search.h” #include

11.7 EXAMPLE PROGRAM 2: A C Program that Creates, Sort, and Searches a One-Dimensional Array of Integers

11.8 HIGHT –DIMENSIONAL ARRAYS An array of one-dimensional arrays is called a two-dimensional arrays; An array of two-dimensional arrays is called a three-dimensional array,and so on. A two-dimensional arrays is equivalent to a table of a fixed number of rows and a fixed number of columns.

Declaring Two-Dimensional Arrays int test_scores[4][3]; The first subscript is the number of rows, and the second is the number of columns. When the complier encounters the declaration for a two- dimensional array,it allocates memory locations for its elements in a linear fashion. The memory locations for the elements on row 0 are followed by the memory locations for the elements on row 1;

Initialization of Two- Dimensional arrays int test_scores[4][3]= {95,80,78,69,75,81,100,98,100,98,85,87}; int test_scores[][3]= {{95,80,78},{69,75,81},{100,98,100},{98, 85,87}};

Operations on Two–Dimensional Arrays If we declare a two-dimensional array in the formal parameter list as int b [][], Each element of the one-dimensional array is a one-dimensional array. We must also tell the complier about the number of elements in each row of the two-dimensional array. Therefore in the formal parameter list the proper declaration must be as int b[][20],thus enabling the complier to compute the offset for the second row as 2*20=40.

The rules for passing two- dimensional arrays to functions 1. Be declared void input_tale(int *no_of _rows, int *no_of_columns, int arr[][COLUMNS_SIZE]);

2.in its prototype. void input_tale(int *no_of _rows, int *no_of_columns, int arr[][COLUMNS_SIZE]);

3. In a call input_table(&rows, &columns, test_scores );

Two-Dimensional Arrays and Pointers int t[4][3] t as a pointer to row 0. *t a pointer to t[0][0]. We can access its content by again applying the indirection operator as *(*t). it is *t+1,and the reference to t[0][1] in pointer/offset notion is *(*t+1).

The pointer to row 1 is t+1. For example, the pointer to the third element in row 1 is *(t + 1 ) +2,and the name of that element is *(*(t + 1) +2). *(*( t + i ) + j) is the name of the array element t[i][j] in pointer/offset notation

for (i=0; i<4; i++) { printf (“\nROW %d OF array t: “, i); for (j=0 ; j<3 ; j++) printf (“%d”, t[i][j]); /*end for */ } /* end for */

for (i=0 ; i < 4 ; i++ ) { printf (“\nROW %d OF array t: “, i); for (j=0 ; j<3 ; j++) printf (“%d”, *(* t + i ) + j ); /*end for */ } /* end for */

Suggest that you avoid using pointer/offset notation unless program efficiency for compilation turns out to be very important in you application.

11.9 EXAMPLE PROGRAM 3: A C Function that Generates a Bar Chart