Sie sind auf Seite 1von 11

:

.
public class Binary_search { public static void main(String[] args) { int[] intArray = {1,3,5,7,12,15,17,19,22,111}; int searchValue = 0, index; System.out.println("Enter 10 numbers");

System.out.print("Enter a number to search for: "); searchValue=16; index = binarySearch(intArray, searchValue); if (index != -1) { System.out.println("Found at index: " + index); } else { System.out.println("Not Found"); } } static int binarySearch(int[] search, int find) { int start, end, midPt; start = 0; end = search.length - 1; while (start <= end) { midPt = (start + end) / 2; if (search[midPt] == find) { return midPt; } else if (search[midPt] < find) { start = midPt + 1; } else { end = midPt - 1; } } return -1; } }

MergeSort
.
public class Merge_sort { /** * The main method illustrates the use of a merge sort to sort a * small array. * The <CODE>String</CODE> arguments (<CODE>args</CODE>) are not used * in this implementation. **/ public static void main(String[ ] args) { final String BLANKS = " "; // A String of two blanks int i; // Array index int[ ] data = { 1000, 80, 10, 50, 70, 60, 90, 20, 30, 40, 0, -1000 }; // Print the array before sorting: System.out.println("Here is the entire original array:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); // Sort the numbers, and print the result with two blanks after each number. mergesort(data, 0, data.length); System.out.println("I have sorted all but the first and last numbers."); System.out.println("The numbers are now:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); }

public static void mergesort(int[ ] data, int first, int n) { int n1; // Size of the first half of the array int n2; // Size of the second half of the array if (n > 1) { // Compute sizes of the two halves n1 = n / 2; n2 = n - n1; mergesort(data, first, n1); // Sort data[first] through data[first+n1-1] mergesort(data, first + n1, n2); // Sort data[first+n1] to the end // Merge the two sorted halves. merge(data, first, n1, n2); }

} private static void merge(int[ ] data, int first, int n1, int n2) // Precondition: data has at least n1 + n2 components starting at data[first]. The first // n1 elements (from data[first] to data[first + n1 1] are sorted from smallest // to largest, and the last n2 (from data[first + n1] to data[first + n1 + n2 - 1]) are also // sorted from smallest to largest. // Postcondition: Starting at data[first], n1 + n2 elements of data // have been rearranged to be sorted from smallest to largest. // Note: An OutOfMemoryError can be thrown if there is insufficient // memory for an array of n1+n2 ints. { int[ ] temp = new int[n1+n2]; // Allocate the temporary array int copied = 0; // Number of elements copied from data to temp int copied1 = 0; // Number copied from the first half of data int copied2 = 0; // Number copied from the second half of data int i; // Array index to copy from temp back into data // Merge elements, copying from two halves of data to the temporary array. while ((copied1 < n1) && (copied2 < n2)) { if (data[first + copied1] < data[first + n1 + copied2]) temp[copied++] = data[first + (copied1++)]; else temp[copied++] = data[first + n1 + (copied2++)]; } // Copy any remaining entries in the left and right subarrays. while (copied1 < n1) temp[copied++] = data[first + (copied1++)]; while (copied2 < n2) temp[copied++] = data[first + n1 + (copied2++)]; // Copy from temp back to the data array. for (i = 0; i < n1+n2; i++) data[first + i] = temp[i]; } }

BubleSort
.

public class Bubble_Sort{ public static void main(String a[]){ int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); } public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; } } } } }

QuickSort
.
public class QuickSort{ public static void main(String a[]){ int i; int array[] = {12,9,4,99,120,1,3,10,13}; System.out.println("\n\n RoseIndia\n\n"); System.out.println(" Quick Sort\n\n"); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); quick_srt(array,0,array.length-1); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); } public static void quick_srt(int array[],int low, int n){ int lo = low; int hi = n; if (lo >= n) { return; } int mid = array[(lo + hi) / 2]; while (lo < hi) { while (lo<hi && array[lo] < mid) { lo++; } while (lo<hi && array[hi] > mid) { hi--; } if (lo < hi) { int T = array[lo]; array[lo] = array[hi]; array[hi] = T; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } quick_srt(array, low, lo); quick_srt(array, lo == low ? lo+1 : lo, n); } }

Closest Pair
.
import java.util.*; /** * @author Kunuk Nykjaer * updated version 1.1 after a bug was found * Divide and conquer implementation */ public class ClosestPair { public static void main(String[] args) throws Exception { // Load your own data for testing P[] points = new P[] { new P(2, 7), new P(4, 13), new P(5, 7),new P(10, 5), new P(13, 9), new P(15, 5), new P(17, 7), new P(19, 10), new P(22, 7), new P(25, 10), new P(29, 14), new P(30, 2) };

Arrays.sort(points, xComparator); // sort by x, then y P[] closest = findClosest(points); P[] closestx = findMinDist( findMinDistNeighbor(points),closest ); for (P p : closestx) System.out.println(p); System.out.println("dist: "+distance(closestx[0],closestx[1])); } /** * Find min distance for neightbors in sorted by x-coord point list * Fix divide problem where information is lost in algorithm * @param ps * @return * * O(n) */ static P[] findMinDistNeighbor(P[] ps) { double minDist = Double.MAX_VALUE; P[] pMin = new P[]{new P(0,0),new P(Double.MAX_VALUE,Double.MAX_VALUE)}; if(ps.length<4) return pMin; for (int i = 0; i < ps.length-3; i++){ P p1 = ps[i]; P p2 = ps[i+1]; P p3 = ps[i+2]; P p4 = ps[i+3]; double dist1 = distance(p1,p2); double dist2 = distance(p1,p3); double dist3 = distance(p1,p4);

if(dist1<minDist){ // update minDist = dist1; pMin = new P[] {p1,p2}; } if(dist2<minDist){ // update minDist = dist2; pMin = new P[] {p1,p3}; } if(dist3<minDist){ // update minDist = dist3; pMin = new P[] {p1,p4}; } } return pMin; }

static P[] findMinDist(P[] p1,P[] p2) { double d1 = distance(p1[0],p1[1]); double d2 = distance(p2[0],p2[1]); return d1 < d2 ? p1 : p2; } /** * Closest pair O(nlogn) * Ashish Sharma * Rengakrishnan Subramanian * November 28, 2001 * http://www.cis.ksu.edu/~subbu/Papers/Closest%20pair.pdf * @throws Exception */ static P[] findClosest(P[] ps) throws Exception { // ps must be sorted in x, then y int n = ps.length; if (n <= 3){ return shortest(ps); } else { int left = n / 2; int right = n / 2 + n % 2; // the set datas P[] Pleft = new P[left]; P[] Pright = new P[right]; P[] Pleftmin, Prightmin, Pclosest; for (int i = 0; i < left; i++) Pleft[i] = ps[i]; for (int i = 0; i < right; i++) Pright[i] = ps[i + left]; Pleftmin = findClosest(Pleft); Prightmin = findClosest(Pright); Pclosest = mergePlanes(Pleftmin, Prightmin); return Pclosest;

} } static P[] mergePlanes(P[] p1, P[] p2) throws Exception { if(p1.length>2 || p2.length>2) throw new Exception("Invalid state in mergePlanes"); double d1 = distance(p1[0],p1[1]); double d2 = distance(p2[0],p2[1]); double D = d1 < d2 ? d1 : d2; // delta // minimum P[] pMin = d1 < d2 ? p1 : p2; // default either in left or right sub-plane // examine for possible min dist where // one point is in left sub-plane and one point is in right sub-plane for (int i = 0; i < p1.length; i++) { for (int j = 0; j < p2.length; j++) { P pi = p1[i]; P pj = p2[j]; if (pi.equals(pj)) continue; double xi = p1[i].getX(); double xj = p2[j].getX(); double yi = p1[i].getY(); double yj = p2[j].getY(); if (xi < xj + D && yi + D > yj && yj > yi - D) { if ( distance(pi,pj) < D) { return new P[]{ pi, pj }; } } } } // either both points were in left or right sub-plane return pMin; } // O(n^2) naive version of closest pair static P[] shortest(P[] ps) { P p1 = null; P p2 = null; double distance = Double.MAX_VALUE; for (int i = 0; i < ps.length; i++) { for (int j = 0; j < i; j++) { if (i == j) continue; P ptemp1 = ps[i]; P ptemp2 = ps[j]; if (ptemp1.equals(ptemp2)) continue;

double newDistance = distance(ptemp1, ptemp2); if (newDistance < distance) { // update distance = newDistance; p1 = ptemp1; p2 = ptemp2; } } } P[] points = new P[]{ p1, p2}; return points; } static P[] union(P[] ps1, P[] ps2) { P[] ps = new P[ps1.length + ps2.length]; for (int i = 0; i < ps1.length; i++) ps[i] = ps1[i]; for (int i = 0; i < ps2.length; i++) ps[i + ps1.length] = ps2[i]; return ps; } static double distance(P p1, P p2) { return p1.distance(p2); // Java api, Euclidean dist } static final Comparator<P> xComparator = new Comparator<P>() { @Override public int compare(P a, P b) { if (a.x < b.x) { return -1; } if (a.x > b.x) { return 1; } // if equal, sort by y if (a.y < b.y) { return -1; } if (a.y > b.y) { return 1; } return 0; } }; }

import java.awt.geom.Point2D; /** * @author Kunuk Nykjaer */ public class P extends Point2D.Double { public String name; public P(double x, double y, String name) { super(x,y); this.name = name; } public P(double x, double y) { this(x,y,x+"_"+y); } public String show() { int i = (int)(Math.round(x)); int j = (int)(Math.round(y)); return name+" ["+i+";"+j+"]"; } @Override public String toString() { return "("+x+";"+y+")"; } @Override public boolean equals(Object o) { P p = (P)o; return this.name.equals(p.name); } }

Find Min_Max
.
public class Min_Max { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int [] niza={2,5,3,6,4,7,1,10,11,12}; int min,max,i,a,b; int size=niza.length; min=niza[0]; max=niza[0]; for (i=0;i<size/2;i++) { if (niza[i]<niza[i+size/2]) { a=niza[i]; b=niza[i+size/2]; } else { b=niza[i]; a=niza[i+size/2]; } if (min>a) min=a; if (max<b) max=b; } System.out.println("Minimum e "+ min); System.out.println("Maximum e "+ max); } }

Das könnte Ihnen auch gefallen