Sort Numbers : Sort Search : Collections Data Structure : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  Collections Data Structure   » [  Sort Search  ]  Screenshots 
 



Sort Numbers



/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

/**
 * This class demonstrates how to sort numbers using a simple algorithm
 */
public class SortNumbers {
  /**
   * This is a very simple sorting algorithm that is not very efficient when
   * sorting large numbers of things
   */
  public static void sort(double[] nums) {
    // Loop through each element of the array, sorting as we go.
    // Each time through, find the smallest remaining element, and move it
    // to the first unsorted position in the array.
    for (int i = 0; i < nums.length; i++) {
      int min = i; // holds the index of the smallest element
      // find the smallest one between i and the end of the array
      for (int j = i; j < nums.length; j++) {
        if (nums[j< nums[min])
          min = j;
      }
      // Now swap the smallest one with element i.
      // This leaves all elements between 0 and i sorted.
      double tmp;
      tmp = nums[i];
      nums[i= nums[min];
      nums[min= tmp;
    }
  }

  /** This is a simple test program for the algorithm above */
  public static void main(String[] args) {
    double[] nums = new double[10]// Create an array to hold numbers
    for (int i = 0; i < nums.length; i++)
      // Generate random numbers
      nums[i= Math.random() 100;
    sort(nums)// Sort them
    for (int i = 0; i < nums.length; i++)
      // Print them out
      System.out.println(nums[i]);
  }
}


           
       
Related examples in the same category
1.  Simple Sort Demo Simple Sort Demo
2.  A simple applet class to demonstrate a sort algorithm
3.  Sorting an array of Strings Sorting an array of Strings
4.  Simple version of quick sort Simple version of quick sort
5.  Combine Quick Sort Insertion Sort Combine Quick Sort Insertion Sort
6.  Quick sort with median-of-three partitioning Quick sort with median-of-three partitioning
7.  Selection sort Selection sort
8.  Insert Sort for objects Insert Sort for objects
9.  Insert sort Insert sort
10.  Bubble sort Bubble sort
11.  Merge sort Merge sort
12.  Binary Search Binary Search
13.  Shell sort Shell sort
14.  Topological sorting Topological sorting
15.  Heap sort Heap sort
16.  A quick sort demonstration algorithm A quick sort demonstration algorithm
17.  Customized Sort Test
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.