The two types of arrays that are written by themselves are sorted according to the requirements of the book

Source: Internet
Author: User

 Public classArraySort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intSortnumber = 5; intSortcounta = 0; intSORTCOUNTB = 0; intArraysourcea[] = {6, 3, 21, 43, 5667, 7890, 123, 4, 4323, 5453, 2323, 45245, 3213, 134143, 1234};  for(inti = 0; i < arraysourcea.length; i++) {             for(intj = i + 1; J < Arraysourcea.length; J + +){                if(Arraysourcea[i] <Arraysourcea[j]) {                    inttemp; Temp=Arraysourcea[j]; ARRAYSOURCEA[J]=Arraysourcea[i]; Arraysourcea[i]=temp; } Sortcounta++; } Sortcounta++; }                intArraysourceb[] = {6, 3, 21, 43, 5667, 7890, 123, 4, 4323, 5453, 2323, 45245, 3213, 134143, 1234}; int[] ARRAYSORTB =New int[Sortnumber];  for(inti = 0; i < arraysourceb.length; i++) {            if(I <Sortnumber) {Arraysortb[i]=Arraysourceb[i]; if(i = = (sortNumber-1)){                     for(intj = 0; J < Arraysortb.length; J + +) {                         for(intK = j + 1; K < Arraysortb.length; k++) {                            if(Arraysortb[j] <Arraysortb[k]) {                                inttemp; Temp=Arraysortb[k]; ARRAYSORTB[K]=Arraysortb[j]; ARRAYSORTB[J]=temp; } SORTCOUNTB++; } SORTCOUNTB++; }                    }            } Else {            if(Arraysourceb[i] > Arraysortb[sortnumber-1]) {Arraysortb[sortnumber-1] =Arraysourceb[i];  for(intj = 0; J < Arraysortb.length; J + +) {                     for(intK = j + 1; K < Arraysortb.length; k++) {                        if(Arraysortb[j] <Arraysortb[k]) {                            inttemp; Temp=Arraysortb[k]; ARRAYSORTB[K]=Arraysortb[j]; ARRAYSORTB[J]=temp; } SORTCOUNTB++; } SORTCOUNTB++; } }} SORTCOUNTB++; }                 for(inti = 0; i < arraysortb.length; i++) {System.out.println (arraysortb[i]); } System.out.println ("A sort count is:" +Sortcounta); System.out.println ("A result is:" + arraysourcea[sortnumber-1]); System.out.println ("B sort count is:" +SORTCOUNTB); System.out.println ("B result is:" + arraysortb[sortnumber-1]); }}

Output:

134143
45245
7890
5667
5453
A Sort Count is:120
A result is:5453
B Sort Count is:135
B result is:5453

15 elements as if the B algorithm is not efficient, but how to get the elements to 29?

134143
134143
45245
45245
7890
A Sort Count is:435
A result is:7890
B Sort Count is:209
B result is:7890

Further, how about 57 elements? is the B algorithm getting better?

134143
134143
134143
134143
45245
A Sort Count is:1653
A result is:45245
B Sort Count is:282
B result is:45245

The two types of arrays that are written by themselves are sorted according to the requirements of the book

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.