Customized Sort Test : Sort Search « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Collections Data Structure » Sort SearchScreenshots 
Customized Sort Test

/**
 @version 1.20 07 Apr 1998
 @author Cay Horstmann
 */
public class EmployeeSortTest {
  public static void main(String[] args) {
    Employee[] staff = new Employee[3];

    staff[0new Employee("Harry Hacker"35000);
    staff[1new Employee("Carl Cracker"75000);
    staff[2new Employee("Tony Tester"38000);

    ArrayAlg.shellSort(staff);

    int i;
    for (i = 0; i < staff.length; i++)
      System.out.println(staff[i]);

  }
}

abstract class Sortable {
  public abstract int compareTo(Sortable b);
}

class ArrayAlg {
  public static void shellSort(Sortable[] a) {
    int n = a.length;
    int incr = n / 2;
    while (incr >= 1) {
      for (int i = incr; i < n; i++) {
        Sortable temp = a[i];
        int j = i;
        while (j >= incr && temp.compareTo(a[j - incr]) 0) {
          a[j= a[j - incr];
          j -= incr;
        }
        a[j= temp;
      }
      incr /= 2;
    }
  }
}

class Employee extends Sortable {
  public Employee(String n, double s) {
    name = n;
    salary = s;
  }

  public void raiseSalary(double byPercent) {
    salary *= + byPercent / 100;
  }

  public String getName() {
    return name;
  }

  public double getSalary() {
    return salary;
  }

  public String toString() {
    return name + " " + salary + " ";
  }

  public int compareTo(Sortable b) {
    Employee eb = (Employeeb;
    if (salary < eb.salary)
      return -1;
    if (salary > eb.salary)
      return 1;
    return 0;
  }

  private String name;

  private double salary;
}

           
       
Related examples in the same category
1. Simple Sort DemoSimple Sort Demo
2. A simple applet class to demonstrate a sort algorithm
3. Sorting an array of StringsSorting an array of Strings
4. Simple version of quick sortSimple version of quick sort
5. Combine Quick Sort Insertion SortCombine Quick Sort Insertion Sort
6. Quick sort with median-of-three partitioningQuick sort with median-of-three partitioning
7. Selection sortSelection sort
8. Insert Sort for objectsInsert Sort for objects
9. Insert sortInsert sort
10. Bubble sortBubble sort
11. Merge sortMerge sort
12. Binary SearchBinary Search
13. Shell sortShell sort
14. Topological sortingTopological sorting
15. Heap sortHeap sort
16. Sort NumbersSort Numbers
17. A quick sort demonstration algorithmA quick sort demonstration algorithm
w_w___w___.j_a___v_a___2__s__._c___om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.