DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Information Technology, 3’rd Semester
Outline Array data structure Swapping Sorting Arrays Searching Arrays Emank X Mezank !!
Array data structure Arrays are data structures consisting of data items of the same type. Arrays are “static” entities, in that they remain the same size once they are created. 3 Presented & Prepared by: Mahmoud R. Alfarra
Swapping Swapping the values of two cells or variables is how to change the values with out missing data. 4 Presented & Prepared by: Mahmoud R. Alfarra AB A 1 3 str1str2 str 2 B
Swapping hold = firstvariable firstvariable = secondvariable secondvariable= hold 5 Presented & Prepared by: Mahmoud R. Alfarra Write a method that performs a swap operation HW 2.1
Sorting Arrays Sorting data : arranging the data into some particular order, such as ascending or descending. Sorting data is one of the most important computing applications. 6 Presented & Prepared by: Mahmoud R. Alfarra
Sorting data for ( int pass = 1, pass < arrayLength, pass++ ) for ( int i = 0; i < arrayLength- 1; i++ ) if ( b[ i ] > b[ i + 1 ] ) Swap(b[ i ], b[ i +1] ); 7 Presented & Prepared by: Mahmoud R. Alfarra Write a method that sort an integer array HW 2.2 Write a method that sort a string array HW 2.3 bubble sort
Searching Arrays Often, programmers work with large amounts of data stored in arrays. It might be necessary in this case to determine whether an array contains a value that matches a certain key value. The process of locating a particular element value in an array is called searching. 8 Presented & Prepared by: Mahmoud R. Alfarra
Searching Arrays We discuss two searching techniques The linear search technique : Search element by element. Uses a for structure containing an if structure to compare each element of an array with a search key. Works well for small or unsorted arrays. The binary search technique: The binary search algorithm eliminates half of the elements in the array being searched after each comparison. Works well for large or sorted arrays. 9 Presented & Prepared by: Mahmoud R. Alfarra
The linear search technique for ( int n = 0, n < arrayLength, n++ ) if ( array[ n ] == wanted) return n break the loop return Presented & Prepared by: Mahmoud R. Alfarra Write a program to implement the linear search technique HW 2.4
The binary search technique 11 Presented & Prepared by: Mahmoud R. Alfarra (0+13) / 2 = 6 If (array[6] == 80) return 6 Elseif (array[6] > 80) High = 6 -1 Else Low = 6+1 Middle = (low + high)/2 Wanted = 80 array 0 (7+13) / 2 = 10 If (array[10] == 80) return 10 Elseif (array[10] > 80) High = Else Low = 10+1 (7+9) / 2 = 8 If (array[8] == 80) return 8 Elseif (array[8] > 80) High = 8-1 Else Low =
The binary search technique 12 Presented & Prepared by: Mahmoud R. Alfarra Middle = (low + high)/2 Wanted = 80 array 0 (7+9) / 2 = 8 If (array[8] == 80) return 8 Elseif (array[8] > 80) High = 8-1 Else Low = (9+9) / 2 = 9 If (array[9] == 80) return 8 Elseif (array[9] > 80) High = 9-1 Else Low = return 8 Using the same representation way perform the linear technique HW 2.5
The binary search technique int low = 0 int high = arrayLength – 1 while ( low <= high ) middle = ( low + high ) / 2 if ( key == array[ middle ] ) return middle else if ( key < array[ middle ] ) high = middle - 1 else low = middle + 1; return -1; 13 Presented & Prepared by: Mahmoud R. Alfarra Write a program to implement the binary search technique with sorted and then unsorted array HW 2.6
Emank X Mezank !! قال عمر بن الخطاب رضي الله عنه: (ثلاث يصفين لك ود أخيك: أن تسلم عليه إذا لقيته وتوسع له في المجلس وتدعوه بأحب أسمائه إليه).
Next Lecture Insert & delete elements