Bubble sort High Force lattice

Source: Internet
Author: User
Tags comparable

Import java.util.comparator;/** * Sequencer Interface (Policy mode: Encapsulates an algorithm into a separate class with a common interface so that they can be replaced with each other) * * @author Chengpeng * */interface Sorter {/   * * Sort * * @param list * Array to be sorted */public <t extends comparable<t>> void sort (t[] list);/** * Sort * * @param list * Array to sort * @param comp * Compare two object comparators */public <T> void sort (t[] list, Comparato R<t> comp);} /** * Bubble Sort * * * */public class Bubblesorter implements Sorter {@Overridepublic <t extends comparable<t>> vo ID sort (t[] list) {Boolean swapped = true;for (int i = 1, len = list.length; i < Len && swapped; ++i) {swapped = false;for (int j = 0; j < len-i; ++j) {if (List[j].compareto (List[j + 1]) > 0) {T temp = list[j];list[j] = list[ J + 1];list[j + 1] = Temp;swapped = True;}}} @Overridepublic <T> void sort (t[] list, comparator<t> comp) {Boolean swapped = true;for (int i = 1, len = List . length; I < Len && swapped; ++i) {swapped = false;for (int j = 0; J < Len-i; ++J) {if (Comp.compare (List[j], list[j + 1]) > 0) {T temp = list[j];list[j] = list[j + 1];list[j + 1] = temp;swapped = true;}}}} public static void Main (string[] args) {person P1 = new person (10); person P2 = new person (20); Person P3 = new person (80); person P4 = new person (40); Person[] List = {P1, p2, p3, P4};//new Bubblesorter (). Sort (list, new Personcomparator ()),//ornew Bubblesorter (). Sort (Lis T, new comparator<person> () {@Overridepublic int compare (person O1, person O2) {return o1.score-o2.score;}}); for (int i = 0; i < list.length; i++) {System.out.println (list[i]);}}} class Person {int score;public person (int score) {This.score = score;} Public String toString () {return string.valueof (This.score);}} Class Personcomparator implements comparator<person> {@Overridepublic int compare (person O1, person O2) {return O2. Score-o1.score;}}

Bubble sort High Force lattice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.