ARRAYS.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Capitolo 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Advertisements

CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
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
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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]
Sorting numbers Arrange a list of n numbers a1, a2, a3,..., an in ascending order. A solution: Using Insertion sort. What is Insertion sort? Insertion.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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.
JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
C++ for Engineers and Scientists Second 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.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Arrays Chapter 7.
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Array in C# Array in C# RIHS Arshad Khan
Searching and Sorting Arrays
Single Dimensional Arrays
Chapter 6 Arrays in C++ 2nd Semester King Saud University
An Introduction to Programming with C++ Sixth Edition
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 7 Arrays.
Computer Programming Methodology File Input
Arrays … The Sequel Applications and Extensions
Chapter 8: Collections: Arrays
Java How to Program, Late Objects Version, 10/e
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Continued.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 8 Slides from GaddisText
Lecture 11 Searching and Sorting Richard Gesick.
Know for Quiz Everything through Last Week and Lab 7
Searching and Sorting Arrays
Review of Arrays and Pointers
Lab 4: C Array Review: Array Lab assignments.
Search,Sort,Recursion.
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Engineering Problem Solving with C++, Etter
Lecture Notes – Week 2 Lecture-2
Searching and Sorting Arrays
Arrays Part 2.
To refer to an element, specify
Searching and Sorting Arrays
Arrays.
Sorting Algorithms.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

ARRAYS

Consecutive set of memory locations

By using assignment statement Data type array_name[]= { };

DIMENSIONAL ARRAY A dimensional array is a structure created in memory to represent a number

Single Dimensional : Array having only one subscript variable is called One-Dimensional array It is also called as Single Dimensional Array or Linear array

Multi Dimensional  Array having more than one subscript variable is called Multi-Dimensional array It is also called as Matrix.

Declaring an array Array can be declared in three ways By using assignment statement int a[5]={10,20,30,40,50}; int m[]={1,2,3,4,5,6,7,8,9,10}; char c[]={‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’}; String s[]={“Sonu”,”Ram”,”Ajay”};

By using function argument ( Bluej system) void main(int a[]) void main(char a[]) void main(String a[])

By using input stream int m[] = new int[10]; m[i]=integer.parseInt(in.readLine()); String s[] = new string[10]; S[i]=in.readLine();

OPERATIONS USING ARRAYS

SEARCH This is one of the basic operations It is a process to determine whether a given item is present in the array or not There are two ways SEARCH BINARY LINEAR

LINEAR SEARCH Search begins at the start Continues one after the other Each element is checked and compared with the array element until it reaches the data item

The element is found Enter the array elements one by one 21 36 55 6 87 19 39 45 6 12 Enter the number to be searched 87 The element is found

import java.io.*; class linearsearch { public static void main(String args[]) throws IOException { int i,sh,k=0; int[] m = new int[10]; DataInputStream in = new DataInputStream(System.in); for (i = 0; i <10; i++) System.out.print("Enter the numbers one by one: "); m[i]=Integer.parseInt(in.readLine()); } System.out.print("Enter the number to be searched : "); sh = Integer.parseInt(in.readLine()); for (i = 0; i < 10; i++) if (m[i] == sh) k=1; if(k==1) System.out.println("The number is found " ); else System.out.println("The number is not found");

BINARY SEARCH Takes place only on sorted data items Array is divided into two halves The data item is searched either in the first or in the second half

45 57 69 70 72 79 82 84 91 95 P Last First P = (First + Last)/2 = (1+10)/2 =11/2 =5 84 Number to be searched

= 45 57 69 70 72 79 82 84 91 95 < > P Last First 84

79 82 84 91 95 84

84 < 72 79 82 84 91 95 45 57 69 70 84 72 > 84 72 =

import java.io.*; public class binary { public static void main(String a[])throws IOException DataInputStream in=new DataInputStream(System.in); int i,ns,p,k; k=0; int m[]=new int[10]; int lb=0; int ub=9; for(i=0;i<10;i++)

{ System.out.println("enter the elements of the array in ascending order"); m[i]=Integer.parseInt(in.readLine()); } System.out.println("enter the number to be searched"); ns=Integer.parseInt(in.readLine()); while(lb<=ub) { p=(lb+ub)/2; if(m[p]<ns) lb=p+1; else if(m[p]>ns) ub=p-1; else if (m[p]==ns) k=1; break; if(k==1) System.out.println("found"); else System.out.println("notfound");

Sorting Arranging the data either in ascending or descending order SELECTION SORT BUBBLE SORT

SELECTION SORT USED TO SORT THE DATA ITEMS IN A SPECIFIED ORDER Successive round are executed to select the elements in some order and place them in positions First the smallest number is selected and swaped with the first element and the next smallest and it goes on

12 4 23 8 36 4 12 23 8 36 4 8 23 12 36 4 8 12 23 36

class selec { void x(int a[]) int i,j,min,t; for(i=0;i<9;i++) min=i; for(j=i+1;j<10;j++) if(a[j]<a[min]) min=j; } t=a[i]; a[i]=a[min]; a[min]=t; System.out.println("Array in ascending order"); for(i=0;i<10;i++) System.out.println(a[i]);

BUBBLE SORT Array is sequentially scanned several times Pair of consecutive elements are compared and interchanged

12 4 23 8 36 4 12 23 8 36 4 12 23 8 36 4 12 8 23 36

class bub { void x() int i,j,t; int a[]={2,4,5,7,1,3,6,9,8,10}; for(i=0;i<9;i++) for(j=0;j<=9-1;j++) if(a[j] <a[j+1]){ t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }} for(i=0;i<10;i++) System.out.println(a[i]); } } }