133 lines
8.7 KiB
HTML
133 lines
8.7 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Lab17_CalebFontenot.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.ST5 {color: #9876aa}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
|
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST4 {font-family: monospace; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/lab17_CalebFontenot/src/main/java/com/calebfontenot/lab17_calebfontenot/Lab17_CalebFontenot.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$</span><span class="comment">{packagePath}/${mainClassName}.java to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.lab17_calebfontenot;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Lab17_CalebFontenot {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
|
{
|
|
<span class="literal">final</span> <span class="literal">int</span> NUM_TO_SEARCH_FOR = <span class="number">2</span>;
|
|
<span class="literal">int</span>[] originalArray = <span class="literal">new</span> <span class="literal">int</span>[]{<span class="number">420</span>, <span class="number">41</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">26</span>, <span class="number">2243</span>};
|
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Original array: </span><span class="string">"</span>);
|
|
<span class="ST4">printArray</span>(originalArray);
|
|
<span class="ST4">selectionSort</span>(originalArray);
|
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Sorted array: </span><span class="string">"</span>);
|
|
<span class="ST4">printArray</span>(originalArray);
|
|
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Binary Search</span><span class="string">"</span>);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">-----------------------</span><span class="string">"</span>);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">binarySearchWhile() returns: </span><span class="string">"</span> + <span class="ST4">binarySearchWhile</span>(originalArray, NUM_TO_SEARCH_FOR));
|
|
<span class="ST4">isInArray</span>(<span class="ST4">b</span><span class="ST4">inarySearchWhile</span>(originalArray, NUM_TO_SEARCH_FOR), NUM_TO_SEARCH_FOR);
|
|
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">binarySearchFor() returns: </span><span class="string">"</span> + <span class="ST4">binarySearchFor</span>(originalArray, NUM_TO_SEARCH_FOR));
|
|
<span class="ST4">isInArray</span>(<span class="ST4">b</span><span class="ST4">inarySearchFor</span>(originalArray, NUM_TO_SEARCH_FOR), NUM_TO_SEARCH_FOR);
|
|
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">isInArray</span>(<span class="literal">int</span> arrayResult, <span class="literal">int</span> searchQuery)
|
|
{
|
|
<span class="literal">if</span> (arrayResult == <span class="number">0</span>) {
|
|
System.<span class="ST3">out</span>.println(searchQuery + <span class="string">"</span><span class="string"> is in the array.</span><span class="string">"</span>);
|
|
} <span class="literal">else</span> {
|
|
System.<span class="ST3">out</span>.println(searchQuery + <span class="string">"</span><span class="string"> is not in the array.</span><span class="string">"</span>);
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span>[] <span class="ST2">selectionSort</span>(<span class="literal">int</span>[] arr)
|
|
{
|
|
<span class="comment">// Sort an array by comparing each and every item in the array and swapping them if they are less than the following number</span>
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < arr.<span class="ST5">length</span> - <span class="number">1</span>; ++i) {
|
|
<span class="literal">for</span> (<span class="literal">int</span> j = i; j < arr.<span class="ST5">length</span>; ++j) {
|
|
<span class="literal">if</span> (arr[i] > arr[j]) {
|
|
<span class="literal">int</span> temp = arr[i];
|
|
arr[i] = arr[j];
|
|
arr[j] = temp;
|
|
}
|
|
}
|
|
}
|
|
<span class="literal">return</span> arr;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">printArray</span>(<span class="literal">int</span>[] arr)
|
|
{
|
|
<span class="literal">for</span> (<span class="literal">int</span> i : arr) {
|
|
System.<span class="ST3">out</span>.print(i + <span class="string">"</span> <span class="string">"</span>);
|
|
}
|
|
System.<span class="ST3">out</span>.println();
|
|
}
|
|
|
|
<span class="comment">//replace fors with whiles</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">binarySearchWhile</span>(<span class="literal">int</span>[] ar, <span class="literal">int</span> key)
|
|
{
|
|
<span class="literal">int</span> low = <span class="number">0</span>;
|
|
<span class="literal">int</span> high = ar.<span class="ST5">length</span> - <span class="number">1</span>;
|
|
<span class="literal">while</span> (high >= low) {
|
|
<span class="literal">int</span> mid = (low + high) / <span class="number">2</span>;
|
|
<span class="literal">if</span> (key < ar[mid]) {
|
|
high = mid - <span class="number">1</span>;
|
|
} <span class="literal">else</span> <span class="literal">if</span> (key == ar[mid]) {
|
|
<span class="literal">return</span> mid;
|
|
} <span class="literal">else</span> {
|
|
low = mid + <span class="number">1</span>;
|
|
}
|
|
}
|
|
<span class="literal">return</span> <span class="number">1</span>;
|
|
|
|
}
|
|
<span class="comment">//replace while with for</span>
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">binarySearchFor</span>(<span class="literal">int</span>[] ar, <span class="literal">int</span> key)
|
|
{
|
|
<span class="literal">int</span> low = <span class="number">0</span>;
|
|
<span class="literal">int</span> high = ar.<span class="ST5">length</span> - <span class="number">1</span>;
|
|
<span class="literal">for</span> (low = <span class="number">0</span>; high >= low;) {
|
|
<span class="literal">int</span> mid = (low + high) / <span class="number">2</span>;
|
|
<span class="literal">if</span> (key < ar[mid]) {
|
|
high = mid - <span class="number">1</span>;
|
|
} <span class="literal">else</span> <span class="literal">if</span> (key == ar[mid]) {
|
|
<span class="literal">return</span> mid;
|
|
} <span class="literal">else</span> {
|
|
low = mid + <span class="number">1</span>;
|
|
}
|
|
}
|
|
<span class="literal">return</span> <span class="number">1</span>;
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|