0% found this document useful (0 votes)
2 views

Computer

The document discusses arrays, focusing on single and multi-dimensional arrays, their creation, and basic operations such as searching and sorting. It explains the need for arrays in programming to efficiently store and manipulate multiple data items of the same type. Additionally, it covers various methods for inputting data into arrays and provides examples of code implementations in Java.

Uploaded by

dhruv09agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views

Computer

The document discusses arrays, focusing on single and multi-dimensional arrays, their creation, and basic operations such as searching and sorting. It explains the need for arrays in programming to efficiently store and manipulate multiple data items of the same type. Additionally, it covers various methods for inputting data into arrays and provides examples of code implementations in Java.

Uploaded by

dhruv09agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 18
Arrays Dimensig en 3 = G __ension genni vensionial ar¥ayS, JOUBIe: dime, pi ae 2. die gorting teChMiques (seleg arrays: ty inary Search) © Dita ch and ‘on, cre ot © Int seartl = © Different tasks 0 ar sear column elements, Sum of gg pubble sort), Search . , double dimensional arrays Vi diagonal elements. Introduction As we get on with Javi conomical : ‘the most convenient and coon tans by using minimum set ofa nus date the memory to store numerot ava cl we are going to stress ace. In this chapters SS on opt optimum memory space: pease. a ere eae for effective storage of data with Pp gical g Dimensional Array. ing, it becomes necessary {© mang a Programs way. You may need to create logicgy Need of Using an Array ae ; nel To understand i need of an array, refer to the program segment shown belay e fort ’ imag! for(int i=1;i<=10;i++) i we m = Integer parseInt(in.read-Line()); ; , a ‘As we have already discussed, a variable is assigned a specific ; store data during compilation. Hence, the location represented by m, ms integer value at a time. As the loop repeats and the next value previous value of m gets replaced. On completion of the loop, the 10 numbers which you have entered, only the last value is location and the remaining nine numbers have been deleted from: Suppose, you have to find the sum and average of all the in m. Now you are in a fix because m holds the last number have been deleted and can not be recovered to perform any fur think of using 10 variables for holding 10 different complex that you may not be able to handle the task Imagine, what will happen if 100 such numbers are Well! we suggest you a way. Use a number of ¥; same data type. These variables will differ in their. subscripts with the help of a loop to deal with so created is termed as a Di y Or g s number of values vent 4 gmoasional array 18 a st Created in the memory to repre ne a diame data type with the variables having the same variable name along o/h pts. A dimensional array is also called Subseripted Variable pensional Array as Compost ia Type pime rent data under single y type that represents a number of similar or different da ! : i 1 array is the concept of ‘This is the reason why @ posite data types ia xcion, is called as composite data type. A dimensions | number of vari type. onal array is said t0 be a composite data type. The other comy Ape pensid e non-primitive oF The composite data types are also said to be non-PF les declared with the same di lass and Interfax sence tYPes. 4 dimensional array is basically of two types Dimensional Aray Single Dimensional Array Single Dimensional Array To understand a single dimensional array, you can imagine a train (say, Shatabdi), It has a number of compartments which are numbered as 0, 1, 2, Hence, Shatabdi[2] will indicate the second compartment of Shatabdi train. Similarly, single dimensional array is a structure that is represented along X-axis by using a number of variables with the same name having different subscripts. Single dimensional array is also called as single subscripted variable because each element of the array is represented by using a common Variable name along with a single subscript. Creating a Single Dimensional Array The syntax to create a single dimensional array is 48 shown below: [] = new 31? Since 55 is greater than 31, therefore Step IT the search will further take place in. the first half of the array i.e., if the number to be searched < mid value then set Last = mid ~ 1 Here, Last = 3-1 =2) e : If the number to be searched is greater than the First = Mid + 1. Sep IV: Again find the middle element of either half and First + Last)/2 =+22=1 = Step II pent the Process Frony gy by " ing in “Search jy ue? Step VI: tn case the & oq resulting Une First eater tha co Pinaity, the proce a an arch a0 Differences between Linear Ses pore) takes plsce oni ed and 1 BION, elther in ascending op some items 1. Linear search works of ase pe SS een is divided into 10 alyey ste? se eg ae te ood al = ton index of the array first half oF in array ie, ot and continues tll the last it ecific order. If the elements Sorting ents in a spe a 2 I is a system of arranging array elements ! descending itt alee ae ee fer jphabedce 7 Neng then they can be sorted in ascending in alphabe 8 is S o characters or Strings then you cans the following two ways: Buble Sort ‘Selection Sort Ste’ Selection Sort It is a technique to arrange array elements in a ore order by se specific element from a set of data items. If the cenee are to be sorted jn order then the lowest element is interchanged with the array element ay index. Next lowest element will be interchanged with the element ay and so on until end of array is reached. For example; fi Suppose, a list of 10 elements stored in an array are to be sorted in The stepwise process is illustrated below: Step 1; At first, the smallest number is selected This number is interchanged with the mea i. Position to 0" position and 44 ext smallest. clemen sro?“ “Interchange it with », itor Nd posit iON element : Find the svep 4 NeXt smallest ¢ Interchange it with 3 Ment from Position element 3rd_ position onward (ie., 49 2 ee = 89 o7 4” 7 o* as 2nd 3at an so on 7 step 5 + Find the next smallest element from it with 4" position element ath Position onward (i.e., 56). Interchange 15 7 440 of p 2a Step 6 : Find the next smallest element from it with 5" position element, i | on 1 nd ual 5" position onward (ie. 77). Step 7 : Find the next smaller element same element should remain in th with same element. 1386 1 i Auto Step 8: The last element ne “aly highest element set ending order. A progr Rubble Sort va for sorting elements in a single dimeng, This technique is most widely wsed f 6° several times and during gal pearear storia dense er rity ll rece a them imo the specie order (ecending of Ge660N8 O11 1 Teale aay a em mo he pete we use Takes a Une whey BN» of exchanges are more. ing order by using buby ~ Technique to sort the numbers in ascending OF wane Roses ble sort Let total number of elements in the array are 8 aS Sho : EE ids eas a Aa = : = x 4o sh oh + Co The different steps that take place during this technique are: + Elements 9 and 6 are compared. Both are interchanged as 9 > 6, Cr a ee _ | Thi i ieee * ass. Elements 9 and 24 are compared but not interchanged as 9< F 1p ge 24, ie 6 Mw 3 1 omy om 1* ong an * 3x * Elements 24 and 45 and 2 a 45 and 2 are compared and int 1 interchang ympletion of the first pass se ts the highest element at the e1 6 9 3 u € 2m gt 24 5 4s 45 am gm gh 7 thus, the highest element is pushed to the last position. Similarly, the process is wed to push the second highest number to the second last position and so on. his is the first pass in which the loop runs for 7, ie., (N-1) iterations. In the second the loop will run for 6, ice., (N-2) iterations and so on. ¢. Total number of iterations will take place to carry the complete process will be #1 inserting an element in an array 4s you have already studied, an array contains various elements having different subscripts by using same variable names. You can insert an element in the existing sray at a defined position. In order to perform this task, the following steps can be taken: 1. As an element is to be inserted into the array, you must ensure that the size of. the array is more than the number of the elements it contains. Let, the array size be II and it contains 10 elements as shown below: 0” ged nt gtk sth gh hgh 2 ss 8s 1) an element 18 is to be inserted in position 5 of clements from cell number 5 to 9 towards one a gfe ———————— been vacated. 5 has 10" co ou ws nerve a cot weber SET we | * 1 oF 4 6 3 - eleme’ ‘ 1 » : £ . . $ it 7 sell position 5 ‘1 aa esses Pom 8. Insert the € - p 7 S ay em , a SS ‘nesay a. ee eA 1 leted. Thus, the insertion in the Sth cell is comp! A. sample program to insert an clemen import java.util.*; class insert { + in a set of array elements public static void main(String args{]) ( int i,n.ele.pos; int arr{}=new int cy opentiay in = new Scanne in); e a out printin(“Enter number of array elements”); n=in.nextint(); =i System.out printIn(“Enter array elements”); for(i=0;i System.out printIn(“Enter element to be inserted”); ele=in.nextint(); System. out printin(“Enter_position of insertion”); pos=in.nextint(); for(i=n-1;i>=pos; arrli+1}=arr{i /Mnserting element in defined position arr[pos]=ele; nt; System.out.printIn(“Array elements after insertion”); for(i=0;i

You might also like