<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.inthewings.net/index.php?action=history&amp;feed=atom&amp;title=CIS_3020_Part_8</id>
	<title>CIS 3020 Part 8 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.inthewings.net/index.php?action=history&amp;feed=atom&amp;title=CIS_3020_Part_8"/>
	<link rel="alternate" type="text/html" href="http://wiki.inthewings.net/index.php?title=CIS_3020_Part_8&amp;action=history"/>
	<updated>2026-05-06T07:25:53Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>http://wiki.inthewings.net/index.php?title=CIS_3020_Part_8&amp;diff=2130&amp;oldid=prev</id>
		<title>Jka at 21:43, 26 April 2007</title>
		<link rel="alternate" type="text/html" href="http://wiki.inthewings.net/index.php?title=CIS_3020_Part_8&amp;diff=2130&amp;oldid=prev"/>
		<updated>2007-04-26T21:43:43Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Sequential Search==&lt;br /&gt;
* Suppose that we have an unsorted array of numbers&lt;br /&gt;
* We know that a particular number is in the array&lt;br /&gt;
* We want the position of that number&lt;br /&gt;
===Code===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public int seqSearch(int[] a, int value, int index) {&lt;br /&gt;
  int result;&lt;br /&gt;
  if (index &amp;gt;= a.length) {&lt;br /&gt;
    result = -1;&lt;br /&gt;
  } else if (value == a[index]) {&lt;br /&gt;
    result = index;&lt;br /&gt;
  } else {&lt;br /&gt;
    result = seqSearch(a, value, index + 1);&lt;br /&gt;
  }&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Binary Search==&lt;br /&gt;
* The binary search algorithm takes advantage of the fact that a set of values in an array are ordered&lt;br /&gt;
* We probe into the middle of the array and if we do not find the value that we are looking for, we throw 1/2 of the values away&lt;br /&gt;
===Code===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public int binSearch(int[] a, int target, int first, int last) {&lt;br /&gt;
  int result;&lt;br /&gt;
  int middle;&lt;br /&gt;
  middle = (first + last) / 2;&lt;br /&gt;
  if (last &amp;lt; first) {&lt;br /&gt;
    result = -1;&lt;br /&gt;
  } else if (a[middle] == target) {&lt;br /&gt;
    result = middle;&lt;br /&gt;
  } else if (a[middle] &amp;lt; target) {&lt;br /&gt;
    result = binSearch(a, target, middle + 1, last);&lt;br /&gt;
  } else {&lt;br /&gt;
    result = binSearch(a, target, first, middle - 1);&lt;br /&gt;
  }&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Problem===&lt;br /&gt;
* Note that binary search is fast&lt;br /&gt;
* &amp;#039;&amp;#039;But&amp;#039;&amp;#039;, to use it we must have the values sorted and sorting can be expensive to do&lt;br /&gt;
* If we are only going to make one or two searches, it might not be worth sorting the values&lt;br /&gt;
* If we are going to make repeated searches (student records, bank records, inventory), sorting the values will have great benefit&lt;br /&gt;
==&amp;#039;&amp;#039;for&amp;#039;&amp;#039; loop==&lt;br /&gt;
* The structure of a &amp;#039;&amp;#039;for&amp;#039;&amp;#039; loop is:&lt;br /&gt;
 for (&amp;lt;init&amp;gt;; &amp;lt;bool expr&amp;gt;; &amp;lt;incre&amp;gt;)&lt;br /&gt;
      &amp;lt;statement&amp;gt;;&lt;br /&gt;
===Which should you use?===&lt;br /&gt;
* Is the number of times you will perform some action fixed and will not change?&lt;br /&gt;
* Will you need to perform an action a variable number of times including possibly zero times?&lt;br /&gt;
* Will you need to perform an action a variable number of times including at least once?&lt;br /&gt;
==Selection Sort==&lt;br /&gt;
===Algorithm===&lt;br /&gt;
# For all positions, k, in the array from n down to one&lt;br /&gt;
# Find the largest element in the subarray 0..k&lt;br /&gt;
## Save k as the position of the largest so far&lt;br /&gt;
## For all values of j from k-1 down to 0&lt;br /&gt;
## If the element at j is bigger than the largest so far, save j as the position of the largest so far&lt;br /&gt;
# If the largest element is not in position k then switch the largest element with the one at position k&lt;br /&gt;
===Code===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void selectSort(int[] val) {&lt;br /&gt;
  int k, j, indexOfMax, temp;&lt;br /&gt;
  for (k=val.length-1; k&amp;gt;=1; k--) {&lt;br /&gt;
    indexOfMax = k;&lt;br /&gt;
    for (j=k-1; j&amp;gt;=0; j--) {&lt;br /&gt;
      if (val[j] &amp;gt; val[indexOfMax]) {&lt;br /&gt;
        indexOfMax = j;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    if (indexOfMax != k) {&lt;br /&gt;
      temp = val[k];&lt;br /&gt;
      val[k] = val[indexOfMax];&lt;br /&gt;
      val[indexOfMax] = temp;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Bubble Sort==&lt;br /&gt;
===Algorithm===&lt;br /&gt;
# Repeat&lt;br /&gt;
# For each pair of adjacent array elements do&lt;br /&gt;
## Initialize noExchanges to True&lt;br /&gt;
## For each pair of adjacent array elements do&lt;br /&gt;
# If the values of the pair are out of order then&lt;br /&gt;
## Exchange the values&lt;br /&gt;
## Set noExchanges to False&lt;br /&gt;
===Code===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void bubbleSort(int[] val) {&lt;br /&gt;
  boolean noExchanges;&lt;br /&gt;
  int first, pass, temp;&lt;br /&gt;
  pass = 1;&lt;br /&gt;
  do {&lt;br /&gt;
    noExchanges = true;&lt;br /&gt;
    for (first = 0; first &amp;lt;= val.length - 1 - pass; first++) {&lt;br /&gt;
      if (val[first] &amp;gt; val[first + 1]) {&lt;br /&gt;
        temp = val[first];&lt;br /&gt;
        val[first] = val[first + 1];&lt;br /&gt;
        val[first + 1] = temp;&lt;br /&gt;
        noExchanges = false;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    pass = pass + 1;&lt;br /&gt;
  } while (! noExchanges);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==[[CIS 3020 Part 9]]==&lt;/div&gt;</summary>
		<author><name>Jka</name></author>
		
	</entry>
</feed>