A simple applet class to demonstrate a sort algorithm : Sort Search « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Sort SearchScreenshots 
A simple applet class to demonstrate a sort algorithm
    
/* From http://java.sun.com/docs/books/tutorial/index.html */

/*
 * @(#)SortItem.java  1.17f 95/04/10 James Gosling
 *
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import java.awt.Color;
import java.awt.Graphics;

/**
 * A simple applet class to demonstrate a sort algorithm. You can specify a
 * sorting algorithm using the "alg" attribyte. When you click on the applet, a
 * thread is forked which animates the sorting algorithm.
 
 @author James Gosling
 @version 1.17f, 10 Apr 1995
 */
public class SortItem extends java.applet.Applet implements Runnable {
  /**
   * The thread that is sorting (or null).
   */
  private Thread kicker;

  /**
   * The array that is being sorted.
   */
  int[] arr;

  /**
   * The high water mark.
   */
  int h1 = -1;

  /**
   * The low water mark.
   */
  int h2 = -1;

  /**
   * The name of the algorithm.
   */
  String algName;

  /**
   * The sorting algorithm (or null).
   */
  SortAlgorithm algorithm;

  /**
   * Fill the array with random numbers from 0..n-1.
   */
  void scramble() {
    int[] a = new int[size().height / 2];
    double f = size().width / (doublea.length;
    for (int i = a.length; --i >= 0;) {
      a[i(int) (i * f);
    }
    for (int i = a.length; --i >= 0;) {
      int j = (int) (i * Math.random());
      int t = a[i];
      a[i= a[j];
      a[j= t;
    }
    arr = a;
  }

  /**
   * Pause a while.
   
   @see SortAlgorithm
   */
  void pause() {
    pause(-1, -1);
  }

  /**
   * Pause a while, and draw the high water mark.
   
   @see SortAlgorithm
   */
  void pause(int H1) {
    pause(H1, -1);
  }

  /**
   * Pause a while, and draw the low&high water marks.
   
   @see SortAlgorithm
   */
  void pause(int H1, int H2) {
    h1 = H1;
    h2 = H2;
    if (kicker != null) {
      repaint();
    }
    try {
      Thread.sleep(20);
    catch (InterruptedException e) {
    }
  }

  /**
   * Initialize the applet.
   */
  public void init() {
    String at = getParameter("alg");
    if (at == null) {
      at = "BubbleSort";
    }

    algName = at + "Algorithm";
    scramble();

    setBackground(Color.white);

    resize(100100);
  }

  /**
   * Paint the array of numbers as a list of horizontal lines of varying
   * lenghts.
   */
  public void paint(Graphics g) {
    int[] a = arr;
    int y = size().height - 1;

    // Erase old lines
    g.setColor(Color.lightGray);
    for (int i = a.length; --i >= 0; y -= 2) {
      g.drawLine(arr[i], y, size().width, y);
    }

    // Draw new lines
    g.setColor(Color.black);
    y = size().height - 1;
    for (int i = a.length; --i >= 0; y -= 2) {
      g.drawLine(0, y, arr[i], y);
    }

    if (h1 >= 0) {
      g.setColor(Color.red);
      y = h1 * 1;
      g.drawLine(0, y, size().width, y);
    }
    if (h2 >= 0) {
      g.setColor(Color.blue);
      y = h2 * 1;
      g.drawLine(0, y, size().width, y);
    }
  }

  /**
   * Update without erasing the background.
   */
  public void update(Graphics g) {
    paint(g);
  }

  /**
   * Run the sorting algorithm. This method is called by class Thread once the
   * sorting algorithm is started.
   
   @see java.lang.Thread#run
   @see SortItem#mouseUp
   */
  public void run() {
    try {
      if (algorithm == null) {
        algorithm = (SortAlgorithmClass.forName(algName)
            .newInstance();
        algorithm.setParent(this);
      }
      algorithm.init();
      algorithm.sort(arr);
    catch (Exception e) {
    }
  }

  /**
   * Stop the applet. Kill any sorting algorithm that is still sorting.
   */
  public synchronized void stop() {
    if (kicker != null) {
      try {
        kicker.stop();
      catch (IllegalThreadStateException e) {
        // ignore this exception
      }
      kicker = null;
    }
    if (algorithm != null) {
      try {
        algorithm.stop();
      catch (IllegalThreadStateException e) {
        // ignore this exception
      }
    }
  }

  /**
   * For a Thread to actually do the sorting. This routine makes sure we do
   * not simultaneously start several sorts if the user repeatedly clicks on
   * the sort item. It needs to be synchronoized with the stop() method
   * because they both manipulate the common kicker variable.
   */
  private synchronized void startSort() {
    if (kicker == null || !kicker.isAlive()) {
      scramble();
      repaint();
      kicker = new Thread(this);
      kicker.start();
    }
  }

  /**
   * The user clicked in the applet. Start the clock!
   */
  public boolean mouseUp(java.awt.Event evt, int x, int y) {
    startSort();
    return true;
  }
}

/*
 * @(#)BidirectionalBubbleSortAlgorithm.java 1.6f 95/01/31 James Gosling
 
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for NON-COMMERCIAL purposes and without fee is hereby granted
 * provided that this copyright notice appears in all copies. Please refer to
 * the file "copyright.html" for further important copyright and licensing
 * information.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

/**
 * A bi-directional bubble sort demonstration algorithm SortAlgorithm.java, Thu
 * Oct 27 10:32:35 1994
 
 @author James Gosling
 @version 1.6f, 31 Jan 1995
 */

class BidirectionalBubbleSortAlgorithm extends SortAlgorithm {
  void sort(int[] athrows Exception {
    int j;
    int limit = a.length;
    int st = -1;
    while (st < limit) {
      boolean flipped = false;
      st++;
      limit--;
      for (j = st; j < limit; j++) {
        if (stopRequested) {
          return;
        }
        if (a[j> a[j + 1]) {
          int T = a[j];
          a[j= a[j + 1];
          a[j + 1= T;
          flipped = true;
          pause(st, limit);
        }
      }
      if (!flipped) {
        return;
      }
      for (j = limit; --j >= st;) {
        if (stopRequested) {
          return;
        }
        if (a[j> a[j + 1]) {
          int T = a[j];
          a[j= a[j + 1];
          a[j + 1= T;
          flipped = true;
          pause(st, limit);
        }
      }
      if (!flipped) {
        return;
      }
    }
    pause(st, limit);
  }
}

/*
 * @(#)SortAlgorithm.java 1.6f 95/01/31 James Gosling
 
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for NON-COMMERCIAL purposes and without fee is hereby granted
 * provided that this copyright notice appears in all copies. Please refer to
 * the file "copyright.html" for further important copyright and licensing
 * information.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

/**
 * A generic sort demonstration algorithm SortAlgorithm.java, Thu Oct 27
 * 10:32:35 1994
 
 @author James Gosling
 @version 1.6f, 31 Jan 1995
 */

class SortAlgorithm {
  /**
   * The sort item.
   */
  private SortItem parent;

  /**
   * When true stop sorting.
   */
  protected boolean stopRequested = false;

  /**
   * Set the parent.
   */
  public void setParent(SortItem p) {
    parent = p;
  }

  /**
   * Pause for a while.
   */
  protected void pause() throws Exception {
    if (stopRequested) {
      throw new Exception("Sort Algorithm");
    }
    parent.pause(parent.h1, parent.h2);
  }

  /**
   * Pause for a while and mark item 1.
   */
  protected void pause(int H1throws Exception {
    if (stopRequested) {
      throw new Exception("Sort Algorithm");
    }
    parent.pause(H1, parent.h2);
  }

  /**
   * Pause for a while and mark item 1 & 2.
   */
  protected void pause(int H1, int H2throws Exception {
    if (stopRequested) {
      throw new Exception("Sort Algorithm");
    }
    parent.pause(H1, H2);
  }

  /**
   * Stop sorting.
   */
  public void stop() {
    stopRequested = true;
  }

  /**
   * Initialize
   */
  public void init() {
    stopRequested = false;
  }

  /**
   * This method will be called to sort an array of integers.
   */
  void sort(int[] athrows Exception {
  }
}


/*
 * @(#)QSortAlgorithm.java  1.6f 95/01/31 James Gosling
 *
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

/**
 * A quick sort demonstration algorithm
 * SortAlgorithm.java, Thu Oct 27 10:32:35 1994
 *
 @author James Gosling
 @version   1.6f, 31 Jan 1995
 */
class QSortAlgorithm extends SortAlgorithm {
    void sort(int[] a, int lo0, int hi0throws Exception {
  int lo = lo0;
  int hi = hi0;
  pause(lo, hi);
  if (lo >= hi) {
      return;
  }
  int mid = a[(lo + hi2];
  while (lo < hi) {
      while (lo<hi && a[lo< mid) {
    lo++;
      }
      while (lo<hi && a[hi> mid) {
    hi--;
      }
      if (lo < hi) {
    int T = a[lo];
    a[lo= a[hi];
    a[hi= T;
                lo++;
                hi--;
    pause();
      }
  }
  if (hi < lo) {
      int T = hi;
      hi = lo;
      lo = T;
  }
  sort(a, lo0, lo);
  sort(a, lo == lo0 ? lo+: lo, hi0);
    }

    void sort(int[] athrows Exception {
  sort(a, 0, a.length-1);
    }
}


/*
 * @(#)BubbleSortAlgorithm.java  1.6f 95/01/31 James Gosling
 *
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

/**
 * A bubble sort demonstration algorithm
 * SortAlgorithm.java, Thu Oct 27 10:32:35 1994
 *
 @author James Gosling
 @version   1.6f, 31 Jan 1995
 */
class BubbleSortAlgorithm extends SortAlgorithm {
    void sort(int[] athrows Exception {
  for (int i = a.length; --i>=0)
      for (int j = 0; j<i; j++) {
    if (stopRequested) {
        return;
    }
    if (a[j> a[j+1]) {
        int T = a[j];
        a[j= a[j+1];
        a[j+1= T;
    }
    pause(i,j);
      }
  
    }
}

           
         
    
    
    
  
Related examples in the same category
1. Linear search
2. Animation for quick sort
3. Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
4. Simple Sort DemoSimple Sort Demo
5. Sorting an array of StringsSorting an array of Strings
6. Simple version of quick sortSimple version of quick sort
7. Combine Quick Sort Insertion SortCombine Quick Sort Insertion Sort
8. Quick sort with median-of-three partitioningQuick sort with median-of-three partitioning
9. Fast Quick Sort
10. Selection sortSelection sort
11. Insert Sort for objectsInsert Sort for objects
12. Insert sortInsert sort
13. Bubble sortBubble sort
14. Merge sortMerge sort
15. Fast Merge Sort
16. Binary SearchBinary Search
17. Shell sortShell sort
18. Recursive Binary Search Implementation in Java
19. Topological sortingTopological sorting
20. Heap sortHeap sort
21. Sort NumbersSort Numbers
22. A quick sort demonstration algorithmA quick sort demonstration algorithm
23. Performing Binary Search on Java byte Array Example
24. Performing Binary Search on Java char Array Example
25. Performing Binary Search on Java double Array Example
26. Performing Binary Search on Java float Array Example
27. Performing Binary Search on Java int Array Example
28. Performing Binary Search on Java long Array Example
29. Performing Binary Search on Java short Array
30. Sort items of an array
31. Sort an array of objects
32. Sort a String array
33. Sort string array with Collator
34. Binary search routines
35. Customized Sort Test
36. FastQSorts the [l,r] partition (inclusive) of the specfied array of Rows, using the comparator.FastQSorts the [l,r] partition (inclusive) of the specfied array of Rows, using the comparator.
37. A binary search implementation.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.