Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.

Similar presentations


Presentation on theme: "C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures."— Presentation transcript:

1 C# PROGRAMMING Searching & Sorting

2 Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures to sort and search arrays. (6%)

3 Searching vs. Sorting When you search an array, it is to determine if a specified value is present.  There are two primary searching algorithms 1. Linear Search 2. Binary Search Sorting is done to order the values in the array based upon some key value.  There are three primary sorting algorithms 1. Selection Sort 2. Insertion Sort 3. Merge Sort

4 C# PROGRAMMING Searching Algorithms

5 Linear Search A linear search algorithm searches the array in a sequential manner. The algorithm moves through the array, comparing the key value with the values of the elements. If it does not find the key value, it simply moves to the next element. 5861273 intArray 0123456

6 Linear Search for (int index = 0; index < intArray.Length; index++) { if (array [index] == searchValue) return index; return -1; } Note: The Loop will iterate until the value is found or it reaches the end.

7 Binary Search The binary search algorithm is more efficient than the linear search algorithm. Binary search requires that the array be sorted first. The algorithm splits the array and checks the middle value.  If it is not found it compares the values.  If the search value is higher than the middle value, the algorithm moves to the upper half (now a subarray). (Lower – it moves to the lower half.  It splits the subarray in half, checks the middle for a match.  If not found, it checks to see if it is higher/lower and moves to appropriate subarray.  This continues until it has no more values or finds a match.

8 Binary Search Binary Search Code int low = 0;//starts with beginning position at 0 int high = arr.Length – 1;//starts with end position at last element int mid = (low + high + 1)/2;//calculates middle position int index = -1;//sets index to -1, not found at beginning do { if (searchVal == arr[mid])//checks middle index = mid; else if (searchVal arr[mid])//sets subarray if searchVal > mid low = mid + 1; mid = (low + high + 1)/2;//recalculate middle value } while ((low <= high) && (index ==-1))//continues loop while not found return index;

9 C# PROGRAMMING Sorting Algorithms

10 Selection Sort This is a simple sorting algorithm. It moves through the array looking for the lowest value, then moves it to the front. The second pass through the array, it looks for the second lowest and moves it to the second position. It keeps passing through the array until the last iteration.

11 Selection Sort 54813 14853 13854 13458 13458

12 int smallestVal; for ( int i = 0; i < arrName.Length – 1; i++) { smallestVal = i; for ( int index = i + 1; index < arrName.Length; index ++) { if ( arrName [ index ] < arrName [ smallestVal ]) smallestVal = index; int temp = arrName [ i ]; data [ i ] = data [ smallestVal ]; data [ smallestVal ] = temp; }

13 Insertion Sort Another simple sorting algorithm. 1 st Iteration – Compares element 1 & 2 – Swaps if element 1 > element 2. 2 nd Iteration – Looks at element 3 – Inserts it in position given element 1 & 2. It keeps comparing and inserting until the end.

14 14358 14538 14583 41583 45183 45813 Insertion Sort 54813 45813 13458 13458

15 arr = new int [size]; int insertVal; for(int next = 1; next < arr.Length; next ++) { insertVal = arr[next];//hold value int move = next;//initialize index pos to put element while (move > 0 && arr [move – 1] > insert) { arr [move ] = arr [move – 1]; move --; } arr [move] = insertVal;//insert value }

16 Merge Sort The merge sort algorithm sorts the array by splitting the array into two subarrays, sorting the subarrays, then merging them back together sorted. 56186517352944 56186517352944 56186517352944 56186544173529 1856 1765293544 17185665293544 17182935445665

17 Measuring Efficiency How efficiency an algorithm is can be measured using Big-O notation. Big-O notation measure the worst-case runtime for an algorithm.

18 Array Class There are methods in the Array class that can be used to search and sort an array. You can sort an array in ascending order as follows Array.Sort(arrName); You can sort an array in descending order as well. Array.Reverse(arrName);

19 Array Class You can also use the BinarySearch method to perform a binary search.  Remember the array has to be sorted first.  This method returns the index position of the found item, if not found, -1. Example intPosition = Array.BinarySearch(arrName, search);

20 ArrayList Class There are methods in the ArrayList class that can be used to search and sort an ArrayList. You can sort an array in ascending order as follows ArrListName.Sort(); You can sort an ArrayList in descending order as well. ArrListName.Reverse();

21 ArrayList Class You can also use the BinarySearch method to perform a binary search with an ArrayList.  Remember the ArrayList has to be sorted first.  This method returns the index position of the found item, if not found, -1. Example intIndex = ArrListName.BinarySearch(searchObj);

22 Check Your Understanding Given the following code, what will be the content of the array after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); chest.Add(Potion); chest.Add(Potion); chest.Add(Life); chest.Remove(Potion); chest.Reverse();  Potion, Life

23 Check Your Understanding Given the following code, what will be the value of Location after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); Treasure.Add(Potion); Treasure.Add(Potion); Treasure.Add(Life); Treasure.Remove(Potion); int Location = Treasure.BinarySearch(Life); Location = 1

24 Check Your Understanding Given the following code, what will be the value of Location after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); Treasure.Add(Potion); Treasure.Add(Potion); Treasure.Add(Life); Treasure.Remove(Potion); int Location = Treasure.BinarySearch(Heart); Location = -1

25 Check Your Understanding Given the following code, what will be the contents of the array myHealth after execution? intPlayerPoints = new int [] {451, 249, -377} Array.Sort(intPlayerPoints);  -377, 249, 451

26 Check Your Understanding Given the following code, what will be the value of result after execution? int myLevelPoints = new int [] {203, 478, 1785} Array.Sort(myLevelPoints ); int result = Array.BinarySearch(myLevelPoints, 1785);  2

27 Check Your Understanding Given the following code, what would be the values in the sub-arrays after the second iteration using a MergeSort? int myLevels = new int [] {211, 103, 15, 49, 73, 172}  210 103 1549 73 172//first  210 1031549 73172//second

28 Searching & Sorting For more information on the Array class. http://msdn.microsoft.com/en- us/library/czz5hkty.aspx http://msdn.microsoft.com/en- us/library/czz5hkty.aspx For more information on the ArrayList class. http://msdn.microsoft.com/en- us/library/system.collections.arraylist.aspx http://msdn.microsoft.com/en- us/library/system.collections.arraylist.aspx


Download ppt "C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures."

Similar presentations


Ads by Google