1. Trang chủ
  2. » Thể loại khác

thuat toan timkien nhi phan c++

4 325 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Thuật toán tìm kiếm nhị phân C++
Tác giả Dinh Thu Hien
Trường học Học Viện Kỹ Thuật Quân Sự
Chuyên ngành Kỹ Thuật Máy Tính
Thể loại Đồ án
Định dạng
Số trang 4
Dung lượng 16,22 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

có ai giúp gấp cho em bài này được không, làm wài mà nó chẳng ra như ý cả. Chẳng là thuật toán tìm kiếm nhị phân đó mà. #include<graphics.h> #include<conio.h> #include<stdio.h> #include<math.h> #include<stdlib.h> #include<dos.h> #include<iostream.h> int array[100]; int mh=0,mode=0; //-------------------------------------// void Init_Graph(char*duongdan) { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode,duongdan); errorcode = graphresult(); if (errorcode != grOk) { cout<<"Loi Khoi Tao Do Hoa: \n"<< grapherrormsg(errorcode); getch(); exit(1); } } //----------------------------------- void KhoiTao_Mang(int a[100],int n) { for(int i=0;i<n;i++) { a[i]=random(100); } } //----------------------------------- void In_Mang(int a[100],int n) { for(int i=0;i<n;i++) cout<<a[i]<<" "; } //---------------------------------- void SapXep(int a[100], int n) { for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) { if (a[i]>=a[j]) { int tg=a[i]; a[i]=a[j]; a[j]=tg; } } } //--------------------------------------------- void Ve(int a[100],int n) { int Step; char st[10]; SapXep(a,n); Step=(getmaxx()-40)/n; outtextxy(Step-7,165,"LEFT"); outtextxy(n*Step+Step-30,165,"RIGHT"); for(int i=0;i<n;i++) { outtextxy(i*Step+Step,117,"|"); setfillstyle(9,random(15)); itoa(a[i],st,10); fillellipse(i*Step+Step+5,140,12,12); //circle(i*Step+Step+5,140,12); outtextxy(i*Step+Step,136,st); } int mid=(n)/2; setfillstyle(9,RED); itoa(a[mid],st,10); fillellipse(mid*Step+Step+5,140,12,12); outtextxy(mid*Step+Step,136,st); } //--------------------------------------------- int binarysearch(int a[100],int x,int n) { int left,right,mid; char*tg; left=0; right=n-1; int Step=(getmaxx()-40)/n; while(left<=right) { mid=(left+right)/2; if (a[mid]<x) { left=mid+1; tg="left"; } else { if (a[mid]>x) { right=mid-1; tg="right"; } else return mid; } char*strmid; while (!kbhit()) { //delay(100); gotoxy(5,7);cout<<" Vi tri:"<<mid; setcolor(random(15)); circle(mid*Step+Step+5,140,16); itoa(mid,strmid,10); outtextxy(mid*Step+Step+2,107,strmid); outtextxy(mid*Step+Step+2,165,tg); } getch(); } return -1; } void main() { clrscr(); Init_Graph("c:\\borlandc\\BGI"); //Paint(start,n); int n=15; setbkcolor(BLUE); setcolor(YELLOW); rectangle(2,2,getmaxx()-2,getmaxy()-2); rectangle(5,5,getmaxx()-5,getmaxy()-5); KhoiTao_Mang(array,n); gotoxy(17,2);cout<<"----- MO PHONG THUAT TOAN TIM KIEM NHI PHAN ----"; gotoxy(26,3);cout<<"Dinh Thu Hien - K8A - HVKTQS"; gotoxy(5,4);cout<<"* In Mang ban dau:\n"; gotoxy(5,5); In_Mang(array,n); setcolor(WHITE); gotoxy(5,6);cout<<"* Bieu dien tren truc so:"; line(30,120,getmaxx()-30,120); outtextxy(getmaxx()-30,117,">"); Ve(array,n); int Step=(getmaxx()-40)/n; int kq=binarysearch(array,44,n); char*st; if (kq!=-1) { while (!kbhit()) { // delay(100); gotoxy(5,7);cout<<" Vi tri tim thay:"<<kq; setfillstyle(9,random(15)); itoa(kq,st,10); setcolor(random(15)); circle(kq*Step+Step+5,140,16); outtextxy(kq*Step+Step+2,107,st); outtextxy(kq*Step+Step+2,200,"TIM THAY"); } } else outtextxy(kq*Step+Step+2,200,"KHONG TIM THAY"); getch(); closegraph(); } . KhoiTao_Mang(array,n); gotoxy(17,2);cout<<"----- MO PHONG THUAT TOAN TIM KIEM NHI PHAN ----"; gotoxy(26,3);cout<<"Dinh Thu Hien -

Ngày đăng: 06/11/2013, 01:15

TỪ KHÓA LIÊN QUAN

w