ASDV-Java/Semester 3/Exams/Projects/GenericMergeSortExam.java.html

156 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>GenericMergeSortExam.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
.ST2 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {font-family: monospace; font-style: italic}
.ST6 {color: #ce54b8; font-family: monospace; font-style: italic}
.ST0 {color: #287bde}
.ST5 {color: #ce54b8}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgramingExam2_CalebFontenot\src\main\java\com\mycompany\programingexam2_calebfontenot\GenericMergeSortExam.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
<span class="literal">import</span> java.lang.reflect.Array;
<span class="literal">import</span> java.util.Comparator;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">GenericMergeSortExam</span> {
<span class="literal">public</span> <span class="literal">static</span> &lt;E <span class="literal">extends</span> Comparable&lt;E&gt;&gt; <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list) {
<span class="ST4">mergeSort</span>(list,
<span class="literal">new</span> Comparator&lt;E&gt;() {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(E e1, E e2) {
<span class="literal">int</span> compareResult = ((Comparable&lt;E&gt;) e1).compareTo(e2);
<span class="literal">return</span> compareResult;
}
});
}
<span class="literal">public</span> <span class="literal">static</span> &lt;E&gt; <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list,
Comparator&lt;? <span class="literal">super</span> E&gt; comparator) {
<span class="literal">if</span> (list.<span class="ST5">length</span> &gt; 1) {
<span class="comment">// Merge sort the first half</span>
E[] firstHalf = (E[]) <span class="literal">new</span> Object[list.<span class="ST5">length</span> / 2];
<span class="comment">//copies 1st half of list into array firstHalf</span>
System.<span class="ST4">arraycopy</span>(list, 0, firstHalf, 0, list.<span class="ST5">length</span> / 2);
<span class="ST4">mergeSort</span>(firstHalf, comparator);
<span class="comment">// Merge sort the second half</span>
<span class="literal">int</span> secondHalfLength = list.<span class="ST5">length</span> - list.<span class="ST5">length</span> / 2;
E[] secondHalf = (E[]) <span class="literal">new</span> Object[secondHalfLength];
System.<span class="ST4">arraycopy</span>(list, list.<span class="ST5">length</span> / 2,
secondHalf, 0, secondHalfLength);
<span class="ST4">mergeSort</span>(secondHalf, comparator);
<span class="comment">// Merge firstHalf with secondHalf</span>
E[] temp = <span class="ST4">merge1</span>(firstHalf, secondHalf, comparator);
System.<span class="ST4">arraycopy</span>(temp, 0, list, 0, temp.<span class="ST5">length</span>);
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST1">Merges</span> <span class="ST1">the</span> <span class="ST1">two</span> <span class="ST1">lists</span> <span class="ST1">using</span> <span class="ST1">a</span> <span class="ST1">Comparator</span><span class="ST1">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@param</span> &lt;E&gt; <span class="comment">The</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">the</span> <span class="comment">methods</span> <span class="comment">accepts</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST1">@param</span> list1 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
<span class="comment"> * </span><span class="ST1">@param</span> list2 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
<span class="comment"> * </span><span class="ST1">@param</span> comparator <span class="comment">Comparator</span>
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">a</span> <span class="comment">sorted</span> <span class="comment">new</span> <span class="comment">list</span> <span class="comment">made</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">merged</span> <span class="comment">list1</span> <span class="comment">and</span> <span class="comment">list2</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">static</span> &lt;E&gt; E[]
<span class="ST3">merge1</span>(E[] list1, E[] list2, Comparator&lt;? <span class="literal">super</span> E&gt; comparator) {
<span class="literal">int</span> returnArraySize = list1.<span class="ST5">length</span> + list2.<span class="ST5">length</span>;
E[] returnArray = (E[]) <span class="literal">new</span> Object[returnArraySize];
<span class="literal">int</span> list1Iterator = 0, list2Iterator = 0, returnArrayIterator = 0, iterationsRemaining = 0;
<span class="literal">for</span> (; returnArrayIterator &lt; returnArraySize; ++returnArrayIterator) {
iterationsRemaining = (returnArraySize - returnArrayIterator);
<span class="literal">if</span> (comparator.compare(list1[list1Iterator], list2[list2Iterator]) &lt;= 0) {
returnArray[returnArrayIterator] = list1[list1Iterator];
<span class="literal">if</span> (iterationsRemaining &gt; 1)
returnArray[returnArrayIterator + 1] = list2[list2Iterator];
<span class="literal">if</span> ((list1.<span class="ST5">length</span> - 1) &lt; (list1Iterator)) {
++list1Iterator;
} <span class="literal">else</span> {
++list2Iterator;
}
++returnArrayIterator;
} <span class="literal">else</span> {
returnArray[returnArrayIterator] = list2[list2Iterator];
<span class="literal">if</span> (iterationsRemaining &gt; 1)
returnArray[returnArrayIterator + 1] = list1[list1Iterator];
<span class="literal">if</span> ((list2.<span class="ST5">length</span> - 1) &lt; (list2Iterator)) {
++list2Iterator;
} <span class="literal">else</span> {
++list1Iterator;
}
++returnArrayIterator;
}
<span class="comment">/*</span>
<span class="comment"> if (comparator.compare(list1[list1Iterator], list2[list2Iterator]) == 1) {</span>
<span class="comment"> returnArray[returnArrayIterator] = list2[list2Iterator];</span>
<span class="comment"> ++list2Iterator;</span>
<span class="comment"> ++returnArrayIterator;</span>
<span class="comment"> } else {</span>
<span class="comment"> returnArray[returnArrayIterator] = list1[list1Iterator];</span>
<span class="comment"> ++list1Iterator;</span>
<span class="comment"> ++returnArrayIterator;</span>
<span class="comment"> }</span>
<span class="comment"> */</span>
}
<span class="literal">return</span> returnArray;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
Integer[] list
= {
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
};
<span class="ST4">mergeSort</span>(list);
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; list.<span class="ST5">length</span>; i++) {
System.<span class="ST6">out</span>.print(list[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println();
String[] list1
= {
<span class="string">&quot;</span><span class="string">ABC</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">abc</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">abm</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Anf</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Good</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Bad</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">nice</span><span class="string">&quot;</span>
};
<span class="ST4">mergeSort</span>(list1, <span class="literal">new</span> Comparator&lt;String&gt;() {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(String s1, String s2) {
<span class="literal">return</span> s1.compareToIgnoreCase(s2);
}
});
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; list1.<span class="ST5">length</span>; i++) {
System.<span class="ST6">out</span>.print(list1[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
}
}
</pre></body>
</html>