Posts

Showing posts with the label sorting

binary search in java

Image
Binary Search is the fast way to search a sorted array. The idea is to look at the element in the middle. If the key is equal to that, the search is finished. If the key is less than the middle element, do a binary search on the first half. If it's greater, do a binary search of the second half. public class BinarySearch { private long[] a; private int nElems; public BinarySearch(int max) { a = new long[max]; // create array nElems = 0; } public int size() { return nElems; } public int find(long searchKey) { return recFind(searchKey, 0, nElems - 1); } private int recFind(long searchKey, int lowerBound, int upperBound) { int curIn; curIn = (lowerBound + upperBound) / 2; if (a[curIn] == searchKey) return curIn; // found it else if (lowerBound > upperBound) return nElems; // can't find it else // divide range { if (a[curIn] < searchKey) // in upper half return recFind(searchKey, curI...

sort an array of object in java

Image
The java.util.Arrays class has static methods for sorting arrays, both arrays of primitive types and object types. The sort method can be applied to entire arrays, or only a particular range. For object types you can supply a comparator to define how the sort should be performed. All object types that implement Comparable (ie, defines compareTo() method), can be sorted with using a comparator. import java.util.Arrays; public class sortobjects { public static void main(String[] args) { String names[] = { "Ankit", "OM", "Deepti", "Anu" }; Arrays.sort(names); for (int i = 0; i < names.length; i++) { String name = names[i]; System.out.print("name = " + name + "; "); } Person persons[] = new Person[4]; persons[0] = new Person("Ankit"); persons[1] = new Person("OM"); persons[2] = new Person("Deepti"); persons[3] = new Person("Anu"); ...

Quick sort in java

Image
Quick sort algorithm is developed by C. A. R. Hoare. Quick sort is a comparison sort. The working of quick sort algorithm is depending on a divide-and-conquer strategy. A divide and conquer strategy is dividing an array into two sub-arrays. Quick sort is one of the fastest and simplest sorting algorithm. The complexity of quick sort in the average case is Θ(n log(n)) and in the worst case is Θ(n2). Working of quick sort algorithm: Input:12 9 4 99 120 1 3 10 13 Output:1 3 4 10 12 13 99 120 The code of the program : 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(" 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("\nValues after the sort:\n\n...

Sorting an array of integers in java

In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. Summaries of popular sorting algorithms Bubble sort Insertion sort Shell sort Merge sort Heap sort Quick sort Bucket sort Radix sort Distribution sort Shuffle sort Make a class named whatever you want like as public class SortNumbers { public static void sort(int[] nums) { for (int i = 0; i < nums.length; i++) { int min = i; for (int j = i; j < nums.length; j++) { if (nums[j] < nums[min]) min = j; } int tmp; tmp = nums[i]; nums[i] = nums[min]; nums[min] = tmp; } } public static void main(String[] args) { int[] nums = new int[args.length]; // Create an array to hold numbers for (int i = 0; i < nums.length; i++) nums[i] = Integer.parseInt(args[i]); sort(nums); // Sort them for (int i = 0; i < nums.length; i++) // Pr...