package unit7; /** * class with several exercises where the divide and conquer technique is * applied to solve different problems such as merge-sort or quick-sort algorithms, * find the maximum element into an array, etc * @author isegura * */ public class DCExercises { //method main to try the different methods (exercises) public static void main(String args[]) { //int A[]={1,4,3,2,5}; int A[]={10,2,1,8,5,3,4,7}; System.out.println(findMax(A)); merge_sort(A); //quicksort(A); for(int x:A) { System.out.print(x+"\t"); } System.out.println(); int x=searchBinary(A,8); System.out.println(x); } /**Given an array of integers A, find the number of ordered pairs (i,j) such as j=i+1 and A(i)>A(j) **/ public static int findConsecutivePairs(int[] A) { if (A==null ||A.length==0) { System.out.println("Error: array is empty"); return -1; } return findConsecutivePairs(A,0,A.length-1); } public static int findConsecutivePairs(int[] A, int start, int end) { if (start>end || start<0 ||end>=A.length) { System.out.println("Error: indexed out of range"); return -1; } if (start==end) return 0; else if (end==start+1) { if (A[start]>A[end]) return 1; else return 0; } else { int m= (start+end)/2; int pairs1=findConsecutivePairs(A,start,m); int pairs2=findConsecutivePairs(A,m+1,end); if (A[m]>A[m+1]) return 1 + pairs1+pairs2; else return pairs1+pairs2; } } /**Given an array of integers A, find the number of ordered pairs (i,j) such as iA(j) **/ public static int findPairs(int[] A) { if (A==null ||A.length==0) { System.out.println("Error: array is empty"); return -1; } return findPairs(A,0,A.length-1); } public static int findPairs(int[] A, int start, int end) { if (start>end || start<0 ||end>=A.length) { System.out.println("Error: indexed out of range"); return -1; } if (start==end) return 0; else { int pairs=0; for (int index=start+1;index<=end;index++) { if (A[start]>A[index]) pairs++; } return pairs + findPairs(A,start+1,end); } } public static void quicksort(int a[]) { doPartition(a,0,a.length-1); } public static void doPartition(int a[],int start, int end) { if (a==null) { System.out.println("array cannot be null!!!"); return; } if (start>=end) return; //int middle = start + (end- start) / 2; int middle = (start + end) / 2; int pivote = a[middle]; System.out.println("pivote:"+pivote); int i=start; int j=end; while (i<=j) { while (a[i]pivote) j--; if (i<=j) { int x=a[i]; a[i]=a[j]; a[j]=x; i++; j--; } } if (startend || start<0 ||end>=A.length) { System.out.println("Error: indexed out of range"); return -1; } if (start==end) return 0; else if (end==start+1) { if (A[start]>A[end]) return 1; return 0; }else { int pairs=0; for (int index=start+1;index<=end;index++) { if (A[start]>A[index]) pairs++; } int m=(end - start)/2; if (A[m]>A[m+1]) pairs++; return pairs + findPairsDiv(A,start+1,m) + findPairsDiv(A,m+1,end) ; } } int searchBinaryIte(int A[], int x, int start, int end) { for (int i=0; iend) return -1; //x does not exist int mid=(start+end)/2; if (xA[mid]) return searchBinary(A,x,mid+1,end); //if (x==A[mid]) return mid; } public static void merge_sort(int A[]) { if (A==null ||A.length==0) { System.out.println("Error: array is empty"); return; } merge_sort(A,0,A.length-1); } public static void merge_sort(int A[], int start, int end) { if (start==end) { //Do nothing!!! it is already ordered } else { int middle=(start+end)/2; merge_sort(A,start,middle); merge_sort(A,middle+1,end); //merge the two sorted halves merge(A,start,middle,end); } } public static void merge(int A[], int l, int m, int r) { //sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays and copy */ int arr1[] = new int [n1]; int arr2[] = new int [n2]; for (int i=0; i