1 / 28

Searching & Sorting

Searching & Sorting. C# Programming . Objective/Essential Standard. Essential Standard 3.00 Apply Advanced P roperties of Arrays Indicator 3.03 Apply procedures to sort and search arrays. (6%). Searching vs. Sorting.

rufus
Download Presentation

Searching & Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Searching & Sorting C# Programming

  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 • Linear Search • Binary Search • Sorting is done to order the values in the array based upon some key value. • There are three primary sorting algorithms • Selection Sort • Insertion Sort • Merge Sort

  4. Searching Algorithms C# Programming

  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. intArray 0 1 2 3 4 5 6

  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 high = mid – 1;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. Sorting Algorithms C# Programming

  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

  12. Selection Sort intsmallestVal; for ( inti = 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. • 1st Iteration – Compares element 1 & 2 – Swaps if element 1 > element 2. • 2nd Iteration – Looks at element 3 – Inserts it in position given element 1 & 2. • It keeps comparing and inserting until the end.

  14. Insertion Sort

  15. Insertion Sort arr = new int [size]; intinsertVal; 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.

  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 followsArray.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. • ExampleintPosition = 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 followsArrListName.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. • ExampleintIndex = ArrListName.BinarySearch(searchObj);

  22. Check Your Understanding • Given the following code, what will be the content of the array after execution? ArrayListTreasure = new ArrayList();HealthItemPotion= 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();HealthItemPotion= new HealthItem(5);HealthItemLife= 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);HealthItemLife= 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?intmyLevelPoints= 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?intmyLevels = new int [] {211, 103, 15, 49, 73, 172} • 210 103 15 49 73 172 //first • 210 103 15 49 73 172 //second

  28. Searching & Sorting For more information on the Array class. 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

More Related