Diễn đàn cntt ĐH-TÔN ĐỨC THẮNG.Thân mời các anh em tham gia để diễn đàn phong phú hơn
Chào mừng bạn ghé thăm diễn đàn, hãy tham gia đăng kí thành viên cùng chúng tôi để có thêm động lực post bài


Join the forum, it's quick and easy

Diễn đàn cntt ĐH-TÔN ĐỨC THẮNG.Thân mời các anh em tham gia để diễn đàn phong phú hơn
Chào mừng bạn ghé thăm diễn đàn, hãy tham gia đăng kí thành viên cùng chúng tôi để có thêm động lực post bài
Diễn đàn cntt ĐH-TÔN ĐỨC THẮNG.Thân mời các anh em tham gia để diễn đàn phong phú hơn
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

full các bài giải sort java

Go down

full các bài giải sort java Empty full các bài giải sort java

Bài gửi by Admin Wed Mar 21, 2012 1:18 am

import java.util.*;
import java.io.*;
public class KiemTraGiuaKy {

/**
* @param i
* @param j
* @param args
*/
public static void swap(int[] a, int j, int i){
int temp=a[i];
a[i]=a[j];
a[j]=temp;

}

public static void heapSoft(int nodes[],int n){
int i=0,x=0,s=0,f=0,cuoiheap=0;
for( i=1;i<n;i++){
x=nodes[i];
s=i;
f=(s-1)/2;
while(s>0 && nodes[f]<x){
nodes[s]=nodes[f];
s=f;
f=(s-1)/2;
}
nodes[s]=x;
}
for(i=n-1;i>0;i--){
cuoiheap=nodes[i];
nodes[i]=nodes[0];
f=0;
if(i==1) s=-1;
else s=1;
if(i>2 && nodes[2]>nodes[1])
s=2;
while(s>=0 && cuoiheap<nodes[s]){
nodes[f]=nodes[s];
f=s;
s=2*f+1;
if(s+1<=i-1&& nodes[s]<nodes[s+1])
s=s+1;
if(s>i-1)
s=-1;
}
nodes[f]=cuoiheap;
}
}

public static void bubble_sort(int[] a){
for(int i=0;i<a.length;i++)
for(int j=1;j<a.length-i;j++)
if(a[j]>a[j-1])
swap(a,j,j-1);
}

public static void insertSort(int[] a,int n){
int x,j;
for(int i=1;i<n;i++){
x=a[i];
for( j=i-1;j>=0&&x<a[j];j--)
a[j+1]=a[j];
a[j+1]=x;
}
}

public static void mergeSoft(int nodes[],int n){
int i,j,k,low1,up1,low2,up2,size;

int[] dstam=new int[n];
size=1;
while(size<n){
low1=0;
k=0;
while(low1+size<n){
low2=low1+size;
up1=low2-1;
up2=(low2+size-1<n) ? low2+size-1 : n-1;
for(i=low1,j=low2;i<=up1 && j<=up2;k++)
if(nodes[i]<=nodes[j])
dstam[k]=nodes[i++];
else
dstam[k]=nodes[j++];
for(;i<=up1;k++)
dstam[k]=nodes[i++];
for(;j<=up2;k++)
dstam[k]=nodes[j++];
low1=up2+1;
}
for(i=low1;k<n;i++)
dstam[k++]=nodes[i];
for(i=0;i<n;i++)
nodes[i]=dstam[i];
size*=2;
}
}

public static void quickSort(int[] a){
for(int i=0;i<a.length;i++)
for(int j=i+1;j<a.length;j++)
if(a[i]>a[j])
swap(a,i,j);

}


public static void main(String[] args) {
// TODO Auto-generated method stub


try
{

Scanner input = new Scanner(new FileInputStream("in.txt"));
PrintWriter output = new PrintWriter(new FileOutputStream("out.txt"));
String s = input.nextLine();
String[] val = null;
val = s.split(" ");
int[] a = new int[val.length];
for (int i=0;i<val.length;i++)
a[i] = Integer.parseInt(val[i]);
//bubble_sort(a);
quickSort(a);
//heapSoft(a,7);
String result = "";
for (int j=0;j<a.length;j++)
result+=a[j]+" ";
result.trim();
System.out.print(result);
output.print(result);
output.flush();
input.close();
output.close();
}
catch(Exception e)
{
System.out.println(e);
}

}

}
//
https://docs.google.com/document/d/1dJYbXLFupmKlNfkRY3_wRwVhnZQkfvXcQ1z63HvDb9E/edit
Admin
Admin
Thiếu Tá
Thiếu Tá

Tổng số bài gửi : 107
Age : 33

https://itam.forumvi.com

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết