Comparison of time and efficiency of multiple bubbling algorithms

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif;" > My humble:</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >package testwebapp;</span>
/** * * @author Luozhonghua * */public class Testmysort {static final int n=200;public static void Main (String[]args) {lon G Begin=system.currenttimemillis (); int []array=new int[n];//{123,115,100,193,194,167,151,183,190,136}; System.out.println ("Before sorting:"); for (int i=0;i<n;i++) {array[i]= (int) (100+math.random () * (100+1));  System.out.print (array[i]+ "");}     System.out.print ("\ n");     Sort ("", array); System.out.println ("After sorting:");  for (int i:array) {System.out.print (i+ "");} Long End=system.currenttimemillis (); System.out.print ("\ n"); SYSTEM.OUT.PRINTLN ("Time Consuming:" + (End-begin));}  static void sort (String desc,int [] array) {int temp=0;int count=0; if (desc.equals ("desc")) {for (int. i=0;i<array.length;i++) {for (int j=i;j<array.length;j++) {if (array[i]<  ARRAY[J]) {temp = Array[i]; Small array[i] = array[j];  Big Array[j] = temp;//small count++;//array[i] = array[i] + array[j];//array[j] = array[i]-array[j];//array[i] = array[i]                  -ARRAY[J]; }}} System.out.println ("Number of comparisons:" +couNT); }else{for (int i=0;i<array.length;i++) {for (int j=i;j<array.length;j++) {if (Array[i]>array[j]) {temp=array[ I];array[i]=array[j];array[j]=temp; count++;}  }} System.out.println ("Number of comparisons:" +count); }}}


Before sorting:
156 101 178 118 193 195 194 109 157 143 167 183 185 122 173 105 132 130 172 191 169 160 149 168 117 200 122 119 110 114 11 7 118 120 198 145 194 158 197 110 197 169 111 180 186 152 185 152 132 110 149 123 196 168 137 139 166 133 142 160 127 183 188 169 190 184 178 117 102 141 101 135 123 186 102 156 175 162 183 128 139 189 141 183 127 195 164 128 191 110 138 142 18 3 135 110 180 132 134 196 131 152 144 114 174 114 193 174 144 190 138 184 197 200 141 195 131 147 149 132 100 133 199 134 137 144 154 125 110 166 116 112 158 180 143 152 181 105 199 119 110 170 168 113 165 153 188 125 137 181 183 155 146 147 18 0 168 154 191 180 184 162 116 151 118 139 102 179 141 115 130 192 163 114 187 116 135 139 173 186 156 161 131 152 134 185 100 189 130 176 122 191 192 127 156 183 170 160 193 167 158 119 139
Comparison number: 5508
After sorting:
100 100 101 101 102 102 102 105 105 109 110 110 110 110 110 110 110 111 112 113 114 114 114 114 115 116 116 116 117 117 11 7 118 118 118 119 119 119 120 122 122 122 123 123 125 125 127 127 127 128 128 130 130 130 131 131 131 132 132 132 132 133 133 134 134 134 135 135 135 137 137 137 138 138 139 139 139 139 139 141 141 141 141 142 142 143 143 144 144 144 145 146 14 7 147 149 149 149 151 152 152 152 152 152 153 154 154 155 156 156 156 156 157 158 158 158 160 160 160 161 162 162 163 164 165 166 166 167 167 168 168 168 168 169 169 169 170 170 172 173 173 174 174 175 176 178 178 179 180 180 180 180 180 181 18 1 183 183 183 183 183 183 183 184 184 184 185 185 185 186 186 186 187 188 188 189 189 190 190 191 191 191 191 192 192 193 193 193 194 194 195 195 195 196 196 197 197 197 198 199 199 200 200
Time: 16


A book:

Import Java.util.random;public class P4_1 {static final int size=200;public static void Bubblesort (int[] a) {int temp; in T count=0; for (int i = 1; i < a.length; i++) {//Compares two adjacent numbers, the larger number is bubbling for (int j = 0; J < A.length-i; j          + +) {if (A[j] > a[j + 1]) {//swap adjacent two number temp=a[j];          A[J]=A[J+1];          A[j+1]=temp;          count++;          }}//System.out.print ("+i+" step to sort results: ")//output per step sort result//for (int k=0;k<a.length;k++)// {//System.out.print ("+a[k]);  Output//}//System.out.print ("\ n"); } System.out.println ("Number of comparisons:" +count); } public static void Main (string[] args) {long begin=system.currenttimemillis (); int[] Shuzu=new int[size];//{ 123,115,100,193,194,167,151,183,190,136}int i; System.out.print ("Array before sorting is: \ n");//output sorted before array for (i=0;i<size;i++) {shuzu[i]= (int) (100+math.random () * (100+1));// Initializes an array of System.out.print (shuzu[i]+ "");} System.out.print ("\ n"); bubblEsort (Shuzu);//Sort Operation System.out.print ("sorted array is: \ n"); for (i=0;i<size;i++) {System.out.print (shuzu[i]+ "");// Output sorted array}system.out.print ("\ n"); long End=system.currenttimemillis (); System.out.println ("Time-consuming:" + (End-begin));}}

The array before the sort is:
193 118 137 130 152 194 109 194 188 156 139 112 137 159 172 199 196 100 166 154 171 104 197 107 197 193 107 163 129 141 18 6 121 156 139 116 154 158 150 134 199 133 149 102 160 135 142 108 179 159 162 138 200 185 183 183 146 131 163 120 182 136 143 141 148 181 187 109 146 166 141 116 147 184 146 128 130 140 178 120 162 143 156 182 142 157 190 187 118 154 130 154 17 2 161 169 149 132 130 162 171 162 172 119 192 188 163 136 148 130 151 157 171 178 120 117 153 115 193 163 101 127 130 113 167 179 136 158 120 117 191 123 103 190 199 157 108 173 104 198 136 184 109 102 118 185 137 185 137 158 181 120 127 185 13 5 141 128 153 132 110 145 195 164 117 197 127 187 190 109 200 140 162 124 134 178 147 164 139 186 100 141 150 144 162 119 115 110 169 193 114 200 116 112 136 103 131 184 178 114 193 126 175
Comparison number: 10367
The sorted array is:
100 100 101 102 102 103 103 104 104 107 107 108 108 109 109 109 109 110 110 112 112 113 114 114 115 115 116 116 116 117 11 7 117 118 118 118 119 119 120 120 120 120 120 121 123 124 126 127 127 127 128 128 129 130 130 130 130 130 130 131 131 132 132 133 134 134 135 135 136 136 136 136 136 137 137 137 137 138 139 139 139 140 140 141 141 141 141 141 142 142 143 143 14 4 145 146 146 146 147 147 148 148 149 149 150 150 151 152 153 153 154 154 154 154 156 156 156 157 157 157 158 158 158 159 159 160 161 162 162 162 162 162 162 163 163 163 163 164 164 166 166 167 169 169 171 171 171 172 172 172 173 175 178 178 17 8 178 179 179 181 181 182 182 183 183 184 184 184 185 185 185 185 186 186 187 187 187 188 188 190 190 190 191 192 193 193 193 193 193 194 194 195 196 197 197 197 198 199 199 199 200 200 200
Time: 32




Comparison of time and efficiency of multiple bubbling algorithms

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.