|
Problem Description:
To find and sort the array elements, and the use of recursive dichotomy.
Complete Example 1:
public class SortDemo
{
public static void main (String [] args)
{
int [] arr = {10,2,300,41,15,6};
for (int a: arr)
{
System.out.print ( "[" + a + "]");
}
// New SortDemo () insertSort (arr).;
new SortDemo () binaryInsertSort (arr).;
System.out.println ();
for (int a: arr)
{
System.out.print ( "[" + a + "]");
}
}
// Sort dichotomy
public void binaryInsertSort (int [] a)
{
for (int i = 1; i < a.length; i ++)
{
int temp = a [i];
int low = 0;
int high = i-1;
while (low < = high)
{
int mid = (low + high) / 2;
if (a [mid] < temp)
{
low = mid + 1;
}
else
{
high = mid-1;
}
}
for (int j = i-1; j> = low; j--)
{
a [j + 1] = a [j];
}
a [low] = temp;
}
}
// Insertion sort
public void insertSort (int [] a)
{
Coordinate preceding element // participate compare elements; int i
// Current element involved in the comparison; int key
for (int j = 1; j < a.length; j ++)
{
key = a [j];
i = j-1;
while (i> = 0 && a [i]> key)
{
a [i + 1] = a [i]; // right one
i -; // i advance
}
a [i + 1] = key;
}
}
}
Complete Example 2:
// Binary search
import java.util *.;
public class BinarySearchDemo
{
public static void main (String [] args)
{
int [] arr = {10,2,300,41,15,6};
Arrays.sort (arr); // sort the array of natural
. //System.out.println(new BinarySearchDemo () method1 (arr, 41));
System.out.println (new BinarySearchDemo () method2 (arr.length-1,0, arr, 41).);
}
// Recursive binary search
public String method2 (int b, int s, int [] a, int g)
{
int m; // Find the coordinates of the median part
if (s < b)
{
m = (b + s) / 2;
if (a [m] == g)
{
return "you are looking for [" + g + "] is ranked in the array size [" + (m + 1) + "] position.";
}
else if (a [m]> g) // if the intermediate value is greater than the target value, comparing the left half,
{
b = m-1; // first part of the highest-coordinate values into the middle to find a
return method2 (b, s, a, g);
}
else // If the intermediate value is smaller than the target value, then the right half of the comparison
{
s = m + 1; // Find the lowest position after part becomes a middle value
return method2 (b, s, a, g);
}
}
return "you are looking for [" + g + "] is not within the array.";
}
// Parameter is an array and the target value
public String method1 (int [] a, int g)
{
Arrays.sort (a); // sort the array of natural
int b = a.length-1; // Find the highest part of Coordinates
int s = 0; // Find the lowest part of Coordinates
int m; // Find the coordinates of the median part
while (b> = s)
{
m = (b + s) / 2;
if (a [m] == g)
{
return "you are looking for [" + g + "] is ranked in the array size [" + (m + 1) + "] position.";
}
else if (a [m]> g) // if the intermediate value is greater than the target value, comparing the left half,
{
b = m-1; // first part of the highest-coordinate values into the middle to find a
}
else // If the intermediate value is smaller than the target value, then the right half of the comparison
{
s = m + 1; // Find the lowest position after part becomes a middle value
}
}
return "you are looking for [" + g + "] is not within the array.";
}
} |
|
|
|