java实现各种排序算法

[Java]代码
view sourceprint?
001 /** 

002  *  

003  */

004 package sortAlgorithm; 

005   

006 import java.io.File; 

007 import java.io.IOException; 

008 import java.sql.Time; 

009 import java.util.Random; 

010   

011 /** 

012  * @author sky 

013  * 该类给出各种排序算法 

014  * 

015  */

016   

017 public class sort{ 

018     private static Integer[] elem(int n){ 

019         int N=n; 

020         Random random=new Random(); 

021         Integer elem[]=new Integer[N]; 

022         for (int i=0;i<N;i++){ 

023             elem[i]=random.nextInt(1000); 

024         } 

025         return elem; 

026     } 

027     public static void main (String Args[]) throws InterruptedException{ 

028         int n=30000; 

029         Integer elem[]=elem(n); 

030         long start,end; 

031           

032         class sort0 extends Thread{ 

033             Integer elem[]; 

034             int n; 

035             sort0(Integer elem[],int n){ 

036                 this.elem=elem; 

037                 this.n=n; 

038             } 

039             public void run(){ 

040                 System.out.println("线程启动"); 

041                 straightInsertSort(elem,n); 

042             } 

043         } 

044           

045         elem=elem(n); 

046         start=System.currentTimeMillis(); 

047         sort0 s1=new sort0(elem,n); 

048           

049         elem=elem(n); 

050         sort0 s2=new sort0(elem,n); 

051         elem=elem(n); 

052         sort0 s3=new sort0(elem,n); 

053         elem=elem(n); 

054         sort0 s4=new sort0(elem,n); 

055         elem=elem(n); 

056         sort0 s5=new sort0(elem,n); 

057         s1.start(); 

058         s2.start(); 

059         s3.start(); 

060         s4.start(); 

061         s5.start(); 

062         s2.join(); 

063         s1.join(); 

064         s3.join(); 

065         s4.join(); 

066         s5.join(); 

067         System.out.println("多线程简单插入排序:"); 

068         end=System.currentTimeMillis(); 

069           

070         System.out.println(end-start); 

071           

072         elem=elem(n); 

073         start=System.currentTimeMillis(); 

074         straightInsertSort(elem,n); 

075         end=System.currentTimeMillis(); 

076         System.out.println("简单插入排序:"); 

077         System.out.println(end-start); 

078           

079         elem=elem(n); 

080         start=System.currentTimeMillis(); 

081         shellSort(elem,n); 

082         end=System.currentTimeMillis(); 

083         System.out.println("希尔排序:"); 

084         System.out.println(end-start); 

085           

086         elem=elem(n); 

087         start=System.currentTimeMillis(); 

088         bubbleSort(elem,n); 

089         end=System.currentTimeMillis(); 

090         System.out.println("冒泡排序:"); 

091         System.out.println(end-start); 

092           

093         /* 

094         elem=elem(n); 

095         start=System.currentTimeMillis(); 

096         quickSort(elem,n); 

097         end=System.currentTimeMillis(); 

098         System.out.println("快速排序:"); 

099         System.out.println(end-start);*/

100           

101         elem=elem(n); 

102         start=System.currentTimeMillis(); 

103         simpleSelectionSort(elem,n); 

104         end=System.currentTimeMillis(); 

105         System.out.println("简单选择排序:"); 

106         System.out.println(end-start); 

107           

108         elem=elem(n); 

109         start=System.currentTimeMillis(); 

110         heapSort(elem,n); 

111         end=System.currentTimeMillis(); 

112         System.out.println("堆排序:"); 

113         System.out.println(end-start); 

114           

115         elem=elem(n); 

116         start=System.currentTimeMillis(); 

117         mergeSort(elem,n); 

118         end=System.currentTimeMillis(); 

119         System.out.println("归并排序:"); 

120         System.out.println(end-start); 

121     } 

122       

123     //显示排序结果 

124     public static <T extends Comparable<? super T>> void show(T[] elem,int n){ 

125         for (int i=0;i<n;i++){ 

126             System.out.print(elem[i]); 

127             System.out.print(' '); 

128         } 

129         System.out.println(); 

130     } 

131     //交换元素 

132     private static <T extends Comparable<? super T>> void swap(T[] elem,int i,int j){ 

133         T tmp=elem[i]; 

134         elem[i]=elem[j]; 

135         elem[j]=tmp; 

136     } 

137     //直接插入排序法,复杂度为O(n^2) 

138     public static <T extends Comparable<? super T>> void straightInsertSort (T elem[],int n){ 

139         for (int i=1;i<n;i++){ 

140             T e=elem[i]; 

141             int j; 

142             for (j=i-1;j>=0 && e.compareTo(elem[j])<0;j--){ 

143                 elem[j+1]=elem[j]; 

144             } 

145             elem[j+1]=e; 

146         } 

147     } 

148     //shell插入排序算法,复杂度为O(n^1.5) 

149     private static <T extends Comparable<? super T>> void  shellInsertHelp(T elem[],int n,int incr){ 

150         for (int i=incr;i<n;i++){ 

151             T e=elem[i]; 

152             int j=i-incr; 

153             for (;j>=0 && e.compareTo(elem[j])<0;j=j-incr){ 

154                 elem[j+incr]=elem[j]; 

155             } 

156             elem[j+incr]=e; 

157               

158         } 

159     } 

160     public static <T extends Comparable<? super T>> void shellSort(T elem[],int n ){ 

161         for (int incr=n/2;incr>0;incr=incr/2){ 

162             shellInsertHelp(elem,n,incr); 

163         } 

164     } 

165     //冒泡排序算法,时间复杂度为O(n^2) 

166     public static <T extends Comparable<? super T>> void bubbleSort(T elem[],int n){ 

167         for (int i=n-1;i>0;i--){ 

168             for (int j=0;j<i;j++){ 

169                 if (elem[j].compareTo(elem[i])>0){ 

170                     swap(elem,i,j); 

171                 } 

172             } 

173         } 

174     } 

175     //快速排序算法,时间复杂度为O(n*log(n)) 

176     private static <T extends Comparable<? super T>> int partition(T elem[],int low,int high){ 

177         while (low<high){ 

178             for (;elem[high].compareTo(elem[low])>=0 && low<high;high--); 

179             swap(elem,high,low); 

180             for (;elem[high].compareTo(elem[low])>=0 && low<high;low++); 

181             swap(elem,high,low); 

182         } 

183         return low; 

184     } 

185     private static <T extends Comparable<? super T>> void quickSortHelp(T elem[],int low,int high){ 

186         if (low<high){ 

187             int pivot=partition(elem,low,high); 

188             quickSortHelp(elem,low,pivot-1); 

189             quickSortHelp(elem,pivot+1,high); 

190         } 

191     } 

192     public static <T extends Comparable<? super T>> void quickSort(T elem[],int n){ 

193         quickSortHelp(elem,0,n-1); 

194     } 

195     //简单选择排序算法,时间复杂度为O(n^2) 

196     public static <T extends Comparable<? super T>> void simpleSelectionSort(T elem[],int n){ 

197         for (int i=0;i<n-1;i++){ 

198             int lowIdx=i; 

199             for (int j=i+1;j<n;j++){ 

200                 if (elem[lowIdx].compareTo(elem[j])>0) 

201                     lowIdx=j; 

202             } 

203             swap(elem,lowIdx,i); 

204         } 

205     } 

206     //堆排序,时间复杂度为O(n*log(n)) 

207     private static <T extends Comparable<? super T>> void heapAdjust(T elem[],int low,int high){ 

208         for (int i=low,lhs=2*i+1 ;lhs<=high;lhs=2*i+1){ 

209             if (lhs<high && elem[lhs].compareTo(elem[lhs+1])<0)lhs++; 

210             if (elem[i].compareTo(elem[lhs])<0){ 

211                 swap(elem,i,lhs); 

212                 i=lhs; 

213             }else break; 

214         } 

215     } 

216     public static <T extends Comparable<? super T>> void heapSort(T elem[],int n){ 

217         //初始化堆 

218         for (int i=(n-2)/2;i>=0;i--){ 

219             heapAdjust(elem,i,n-1); 

220         } 

221         swap(elem,0,n-1); 

222         //排序 

223         for (int i=n-2;i>0;--i){ 

224             heapAdjust(elem,0,i); 

225             swap(elem,0,i); 

226         } 

227     } 

228     //归并排序算法,时间复杂度为O(n*log(n)) 

229     private static <T extends Comparable<? super T>> void simpleMerge(T elem[],T tmpElem[],int low ,int mid, int high){ 

230         int i=low,j=mid+1,k=low; 

231         for (;i<=mid && j<=high;k++){ 

232             if (elem[i].compareTo(elem[j])<=0) 

233                 tmpElem[k]=elem[i++]; 

234             else 

235                 tmpElem[k]=elem[j++]; 

236         } 

237         for (;i<=mid;i++){ 

238             tmpElem[k++]=elem[i]; 

239         } 

240         for (;j<=high;j++){ 

241             tmpElem[k++]=elem[j]; 

242         } 

243           

244         for (;low<=high;low++){ 

245             elem[low]=tmpElem[low]; 

246         } 

247     } 

248     private static <T extends Comparable<? super T>> void mergeHelp(T elem[],T tmpElem[],int low ,int high){ 

249         if (low < high){ 

250             int mid=(low+high)/2; 

251             mergeHelp(elem,tmpElem,low,mid); 

252             mergeHelp(elem,tmpElem,mid+1,high); 

253             simpleMerge(elem,tmpElem,low,mid,high); 

254         } 

255     } 

256     public static <T extends Comparable<? super T>> void mergeSort(T elem[],int n){ 

257         T[] tmpElem=(T[])new Comparable[n]; 

258         mergeHelp(elem,tmpElem,0,n-1); 

259     } 

260 }

你可能感兴趣的:(算法)