This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
package xac_dinh_vi_tri_so_x_trong_file; import java.util.Scanner; import java.io.File; import java.util.Arrays; /** * * @author dieuhb */ public class Main { /** * @param args the command line arguments */ int n,x,k; int []a; public void docfile() { try{ Scanner sc = new Scanner (new File("d:\\solieu.txt")); n= sc.nextInt(); x= sc.nextInt(); a= new int[n+1]; for(int i= 1; i<=n; i++) a[i]= sc.nextInt(); }catch (java.io.FileNotFoundException e) {System.out.print(e);}; for(int i=1; i<=n; i++) System.out.print(a[i] + " "); } // Độ phức tạp : O(nlgn) void xuly() { Arrays.sort(a); int vt=1; while (a[vt]<x && vt<=n) vt++; System.out.print("\n Vi tri gia tri "+ x+ " trong file la " +vt); } void xuly1() // O(nlg) : nhưng cải tiến ở bước xd vị trí (lgn) { Arrays.sort(a); int vt=0; int l=1, r=n; while(l<=r) { if(a[l]>=x) { vt=l; break;}
package display_list_in_sorted_order; import java.util.Arrays; public class Display_List_in_Sorted_Order { private int[] a; // sinh ngẫu nhiên mảng dữ liệu gồm n phần tử public void sinh(int n) { a= new int[n]; for(int i=0;i<n; i++ ) a[i]=(int)(Math.random()*1000); } public void xuat() { String t=""; for(int i=0;i<a.length ; i++ ) t=t + " " + a[i]; System.out.print(t); } public void Display_Sort(int n) { System.out.print("\n\n---- Hien thi danh sach giam dan ----\n\n"); int []dau; dau=new int[n]; for(int i=0; i<n; i++) dau[i]=0; for(int i=0; i<n; i++) { // tránh lấy lại các giá trị max đã lấy trước đó int vtm=0; while(dau[vtm]==1&& vtm<n) vtm++; // nếu đã chọn thì tránh // tìm phần tử min kế tiếp for(int j=1; j<n; j++) if(a[j]>a[vtm] && dau[j]==0) vtm=j; System.out.print(" " + a[vtm] ); // đánh dấu phần tử max đã chọn. dau[vtm]=1; } } public void sapxep() { Arrays.sort(a); // gọi sắp xếp của Array } public static void main(String[] args) { // TODO code application logic here Display_List_in_Sorted_Order m = new Display_List_in_Sorted_Order(); int n= 3000; m.sinh(n); double t1=System.currentTimeMillis(); m.Display_Sort(n); double t2=System.currentTimeMillis();
System.out.print("\n\n ------ Thoi gian thuc hien:" + (t2-t1) + "\n"); // thời gian tính theo mili giây t1=System.currentTimeMillis(); m.sapxep(); m.xuat(); t2=System.currentTimeMillis(); System.out.print("\n\n ------ Thoi gian thuc hien:" + (t2-t1) + "\n"); // thời gian } }
package ghi_du_lieu_xuong_file; import java.io.*; import java.util.Scanner; public class Main { int n,x; public void ghiFile() { try { FileWriter fw = new FileWriter("d:\\solieu1.inp"); BufferedWriter bw = new BufferedWriter(fw); Scanner kb= new Scanner(System.in); System.out.print("Nhap so phan tu can sinh:"); n=kb.nextInt(); System.out.print("Nhap so x can xac dinh vi tri:"); x=kb.nextInt(); bw.write(n + " " + x ); bw.newLine(); for(int i=1; i<=n; i++) { int t= (int)(Math.random()* 10000); bw.write(t + " "); if(i%30==0) bw.write(t + "\n"); } bw.close(); } catch (IOException ex) { ex.printStackTrace(); } System.out.println("\nDa tao file thanh cong!\n"); } public static void main(String[] args) { // TODO code application logic here Main m = new Main(); m.ghiFile(); } }
public String toString() { String s="====>"; for(int i=1; i<=spt; i++) s= s + arr[i] + "--->"; s=s+ " null"; return s; } void append(int x) // them vao cuoi danh sach gia tri x { if (spt== arr.length-1) { System.out.print("\n LIST IS FULL\n"); int []b= new int[(spt*2+1)]; for(int i=1; i<=spt; i++) b[i]=arr[i]; arr=b; } else arr[++spt]=x; } void add(int x, int vt) { // neu danh sach day thi cap phat lai bo nho if (spt== arr.length-1) { System.out.print("\n LIST IS FULL\n"); int []b= new int[(spt*2+1)]; for(int i=1; i<=spt; i++) b[i]=arr[i]; arr=b; } // dich cac phan tu sang phai 1 o, ke tu vi tri vt for(int j=spt; j>=vt;j--) arr[j+1]= arr[j]; // cho x vao vi tri vt arr[vt]=x; spt++; } int get(int i) { if(i<0|| i>=spt) {System.out.print("\n Vi tri khong hop le\n"); return -1;} else return arr[i]; }
int []b= new int[arr.length*2]; for(int j=0;j<spt; j++) b[j] =arr[j]; arr=b; } // thuc hien lenh chen for(int j=spt; j>i; j--) arr[j] =arr[j-1]; arr[i]=x; spt++; } } } public class My_Linked_List { public static void main(String[] args) { // tao danh sach MyListI L1= new MyListI(); int x; Scanner kb= new Scanner(System.in); int i=1; do { System.out.print("nhap so thu " + (i++)+ ":"); x= kb.nextInt(); if(x>0) L1.append(x); } while(x>0); //---------------------------- System.out.print("\n Danh sach vua tao :\n"+ L1); L1.add(300,4); System.out.print("\n Danh sach sau khi chen :\n"+ L1); /* L1.remove(); L1.remove(2); System.out.print("\n Danh sach sau khi xo p.tu cuoi va phan tu thu 2 :\n"+ L1); L1.insert(2,5 ); L1.insert(5,8); System.out.print("\n Danh sach sau khi chen :\n"+ L1); L1.removeDup(); System.out.print("\n Danh sach sau khi xoa trung :\n"+ L1); */ } }
package mystack1; class MyStack1 { private int []a; private int top; static final int MAXSIZE=1000; // so phan tu toi da cua nx // cac thao tac co ban MyStack1() { a= new int [MAXSIZE +1]; top=0; } //kiem tra ngan xep co rong hay ko boolean EmptyS() { return top==0;} // them 1 phan tu x vao dinh ngan xep void PushS(int x) { if(top<MAXSIZE) a[++top]=x ; else System.out.print("\n Ngan xep bi day\n"); } // Lay phan tu o dinh ngan xep ra int PopS() { if (top<1) {System.out.print("\n Ngan xep RONG \n"); return -1;} else return a[top--]; } int PeekS() { if (top<1) {System.out.print("\n Ngan xep RONG \n"); return -1;} else return a[top]; } // Hien thi noi dung ngan xep, chuyen sang dang chuoi public String toString() { String t="--> null\n"; for(int i=1; i<=top; i++) t="--->" + a[i] + t; return t; } }
public class Mystack1 { public static void main(String[] args) { MyStack1 s= new MyStack1(); /* s.PushS(5);s.PushS(3);s.PushS(8); s.PopS();s.PushS(s.PopS()+9); s.PushS(s.PeekS()+7); System.out.print("\n Noi dung ngan xep:\n" + s); */ int n=13234; System.out.print("\n Bieu dien nhi phan cua "+ n+ " la:"); while(n>0) { s.PushS(n%2); n=n/2; } // lay noi dung ngan xep ra in while(!s.EmptyS()) System.out.print(s.PopS()); System.out.println(); } } package mystack2; class Node { int data; Node next; Node(){} // tao ra 1 not co gia tri x Node(int x) { data=x; next=null;} // tao ra 1 not co gia tri x va dung truoc not t Node(int x, Node t){ data =x; next =t;} } class MyStack2 { private Node top; // cac thao tac co ban MyStack2() {top=null;} boolean EmptyS() {return top==null;} void PushS(int x) { top= new Node(x, top); }
public void hn( int n, char a,char b, char c) { Stack_hn S= new Stack_hn(); khoi x= new khoi(n, a,b,c); S.PushS(x); int d=0; while(!S.EmptyS()) { khoi z= S.PopS(); if(z.sd==1) System.out.print("\n " + (++d)+ ": chuyen 1 dia tu " + z.c1 + " sang "+ z.c3); else { x= new khoi(z.sd-1, z.c2, z.c1,z.c3); S.PushS(x); x= new khoi(1, z.c1, z.c2,z.c3); S.PushS(x); x= new khoi(z.sd-1, z.c1, z.c3,z.c2); S.PushS(x); } } } public static void main(String[] args) { // TODO code application logic here Khu_de_qui_thap_ha_noi bb= new Khu_de_qui_thap_ha_noi(); bb.hn1(3,'a', 'b','c'); // goi de qui System.out.print("\n XONG\n\n"); bb.hn(3,'a', 'b','c'); // goi khu de qui System.out.print("\n XONG\n"); } }
p= new Tree(7, new Tree(8), new Tree(5, new Tree(1), null)); q= new Tree (2, new Tree(6), new Tree(9, new Tree(4), new Tree(8))); T1= new Tree(3, q,p); */ T1= new Tree(); T1.taocay(); System.out.print("\n Ket qua duyet tien tu:"); T1.duyettientu(); System.out.print("\n Tong cac not trong cay la:" + T1.tongnot()); System.out.print("\n So la trong cay la:" + T1.demla()); System.out.print("\n So not trong cay la:" + T1.demnot()); System.out.print("\n So not 1 con trong cay la:" + T1.demnot1con()); System.out.print("\n Chieu cao cay la:" + T1.cao()); Scanner kb= new Scanner(System.in); System.out.print("\nnhap gia tri x can tim :"); int x= kb.nextInt(); if(T1.timx(x)) System.out.print("\n Co gia tri " + x +" trong cay"); else System.out.print("\n Khong co gia tri " + x +" trong cay"); System.out.print("\n XONG \n"); } } package my_bstree; import java.util.Scanner; class TNode{ int data; TNode left, right; TNode(int x) { data=x; left=right=null; } TNode(int x, TNode l, TNode r) { data=x; left=l; right=r; } } class BSTree { TNode root; BSTree() {root=null;} BSTree(int x) {root=new TNode(x);}
} } } public class My_BST { public static void main(String[] args) { // TODO code application logic here BSTree T= new BSTree(); //T.taocay(7); T.chen(15);T.chen(25);T.chen(8);T.chen(10);T.chen(45);T.chen(5);T.chen(9);T.chen(3); T.chen(12);T.chen(21);T.chen(38);T.chen(24); System.out.print("\n cay vua tao:"); T.duyet(); if(T.tim(44)) System.out.print("\n co gia tri 44 trong cay\n"); else System.out.print("\n khong co gia tri 44 trong cay\n"); T.xoa(25); System.out.print("\n cay sau khi xoa gia tri 25:"); T.duyet(); } }
else // có hai con { // xoa max con trai TTNode p=rr.left; while (p.right!=null) p=p.right; // tìm max con trái rr.data=p.data; rr.count=p.count; // thay và nốt gốc p.count=1; rr.left= del(p.data,rr.left); // xóa max con trái return rr; } } } void xoa(int x) { root = del(x,root); } void taocay() // dung ki nhap so 0 { root =null; Scanner kb=new Scanner(System.in); int x; while(true) { System.out.print("Nhap gia tri chen vao cay: "); x= kb.nextInt(); if(x==0) break; ChenX(x); } } } public class BST_DUP { public static void main(String[] args) { Scanner kb=new Scanner(System.in); int x; BST_T t = new BST_T(); t.taocay(); System.out.print("\n Ket qua duyet cay:\n "); t.duyet(); System.out.print("\n Nhap gia tri can xoa:"); x= kb.nextInt(); t.xoa(x); System.out.print("\n Ket qua duyet cay sau khi xoa gia tri " + x+ ":\n "); t.duyet(); System.out.print("\n XONG\n "); } }
Merge_Sort( mid+1,high); merge(low,mid,high); } } void Merge_Sort() { Merge_Sort(1, a.length-1); } } public class Sort_array { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here long t1, t2; My_Sort m1= new My_Sort(); m1.sinh(); //m1.in(); t1=System.currentTimeMillis(); m1.Quick_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN QUICK SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep(); t1=System.currentTimeMillis(); m1.Heap_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN HEAP SORT:"); m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); /* m1.chep(); t1=System.currentTimeMillis(); m1.Merge_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN MERGE SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep();
t1=System.currentTimeMillis(); m1.Select_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN SELECT SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep(); t1=System.currentTimeMillis(); m1.Insertion_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN INSERT SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep(); t1=System.currentTimeMillis(); m1.Bubble_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN BUBBLE SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); System.out.print("\n ----XONG---\n"); */ } }
package myqueue; import java.util.Scanner; /** * * @author dieuhb */ class Node { int data; Node next; Node(){} Node(int x){ data=x; next=null;} Node(int x, Node next){ data=x; this.next=next;} } class MyQueue { Node head, tail; //1.Tao hang doi rong MyQueue() {head=tail=null;} //2 Kiem tra hang doi co rong hay ko boolean EmptyQ() { return head==null && tail==null; } void addQ(int x) { Node t= new Node(x); if(head==null && tail==null) head=tail=t; else { tail.next=t; tail=t;} } int RemoveQ() { if(head==null && tail==null) { System.out.print("\n Hang doi rong\n"); return -1; } else { int x= head.data; if (head==tail) head=tail=null; // neu hang doi co 1 phan tu thi ... else head=head.next; return x; } }
public String toString() { String t="\n"; Node p=head; while (p!=null) { t= t + "-->" + p.data; p=p.next;} t=t + "--->null\n"; return t; } public static void main(String[] args) { MyQueue Q= new MyQueue(); Q.addQ(7);Q.addQ(2);Q.addQ(3); Q.addQ(5);Q.addQ(8);Q.addQ(9); System.out.print("\n HANG DOI:" +Q); // Tinh tong cac gia tri trong hang doi int k=0,x; MyQueue Q1= new MyQueue(); while (!Q.EmptyQ()){ x= Q.RemoveQ(); k=k+ x; Q1.addQ(x);} while (!Q1.EmptyQ()) Q.addQ(Q1.RemoveQ()); System.out.print("\n Tong cac gia tri trong hang doi= " + k+ "\n"); System.out.print("\n HANG DOI:" +Q); // Lay phan tu thu k Scanner kb= new Scanner(System.in); MyQueue Q2= new MyQueue(); System.out.print("Nhap vi tri phan tu muon lay:"); int vt= kb.nextInt(); int dem=0; while (!Q.EmptyQ()) { x=Q.RemoveQ(); dem++; if(dem!=vt) Q2.addQ(x); } if(dem<vt) System.out.print("\n Hang doi khong du " + vt+ " phan tu\n"); while (!Q2.EmptyQ()) { x=Q2.RemoveQ(); Q.addQ(x); } System.out.print("\n HANG DOI sau khi lay phan tu thu " + vt +":" +Q); System.out.print("Nhap vi tri phan tu muon them:"); vt= kb.nextInt(); System.out.print("Nhap gia tri can them:"); int xx= kb.nextInt(); Q2= new MyQueue();
dem=0; while (true) { dem++; if(dem==vt) Q2.addQ(xx); else if (Q.EmptyQ()) break; else {x=Q.RemoveQ(); Q2.addQ(x);} } if(dem<vt-1) System.out.print("\n Hang doi khong du " + vt+ " phan tu\n"); while (!Q2.EmptyQ()) { x=Q2.RemoveQ(); Q.addQ(x); } System.out.print("\n HANG DOI sau khi them phan tu " + xx +" tai vi tri " +vt + ":"+Q); //int n=4567; //int h= String.valueOf(n).length(); //System.out.print("\n Do dai = "+ h); } }
package radix_sort; import java.util.Scanner; /** * * @author dieuhb */ class Node { int data; Node next; Node(){} Node(int x){ data=x; next=null;} Node(int x, Node next){ data=x; this.next=next;} } class MyQueue { Node head, tail; //1.Tao hang doi rong MyQueue() {head=tail=null;} //2 Kiem tra hang doi co rong hay ko
boolean EmptyQ() { return head==null && tail==null; } void addQ(int x) { Node t= new Node(x); if(head==null && tail==null) head=tail=t; else { tail.next=t; tail=t;} } int RemoveQ() { if(head==null && tail==null) { System.out.print("\n Hang doi rong\n"); return -1; } else { int x= head.data; if (head==tail) head=tail=null; // neu hang doi co 1 phan tu thi ... else head=head.next; return x; } } } public class Radix_Sort { int a[]; void sinhday() { int n=0; Scanner kb= new Scanner(System.in); System.out.print(" So phan tu can sinh cho day"); n= kb.nextInt(); a= new int[n+1]; for(int i=1; i<=n; i++) a[i]= (int)(Math.random()*1000); } void inday() { System.out.print("\n Noi dung day:\n"); for(int i=1; i<a.length; i++) System.out.print(" " + a[i]); }
package my_hashtable; /** * * @author dieuhb */ import java.util.*; import java.io.*; public class My_Hash_Table { /** * @param args the command line arguments */ final int tablesize=101; String []ht; int num; My_Hash_Table() { ht= new String[tablesize]; num=0; } int hf( String t) // hàm băm xác định vị trí của từ trong bảng băm { int s=0; for(int i=0; i<t.length(); i++) s=s + (int) t.charAt(i); return s % tablesize; } void insert(String t) { if(num<tablesize) { int vt= hf(t); // xác định vị trí trong bảng băm while(true) { if(ht[vt]==null) {ht[vt]=t; break;} // neu co khe trong thi chen vao va thoat else if(ht[vt].equals(t)) break; // đã có giá trị trong bảng thi thoát else vt = (vt+ 1)% tablesize; // tìm ô rỗng để chèn } } else System.out.print("\n FULL HASH TABLE"); }