void swap(int *x,int *y); void bubble_sort(int *a1,int count); //функция пузырьковой сортировки void min_sort(int *a1,int count); //функция сортировки методом выбора void insert_sort(int *a1,int count); // функция сортировки методом вставки void shell_sort(int *x,int count); //функция сортировки методом Шелла void hoare_sort(int *x,int ,int );//функция сортировки методом Хоора void vvod(int *a1,int count); //функция ввода значений массива void out(int *a1,int count); //функция вывода значений массива void file_write(int *a1,int count); void search(int *a, int l,int *b,int m,int *c,int n,int *d,int o,int *e,int p, int v); void search_b(int *a, int l,int *b,int m,int *c,int n,int *d,int o,int *e,int p, int v); void instructions (void); FILE *file; int i,j,v; int step=0;
void main(void) { int *a,l,*b,m,*c,n,*d,o,*e,p; int choice=0; file=fopen("result_sort.txt","w"); instructions(); // вывести меню scanf("%d", &choice); while (choice !=9) { switch (choice) { case 1: //Initilization arrays puts("Enter length of array a[]:"); //a scanf("%d",&l); a=new int[l]; puts("Enter elements of array"); vvod(a,l);
puts("Enter length of array b[]:"); //b scanf("%d",&m); b=new int[m]; puts("Enter elements of array"); vvod(b,m);
puts("Enter length of array c[]:"); //c scanf("%d",&n); c=new int[n]; puts("Enter elements of array"); vvod(c,n);
puts("Enter length of array d[]:");//d scanf("%d",&o); d=new int[o]; puts("Enter elements of array"); vvod(d,o);
puts("Enter length of array e[]:"); //e scanf("%d",&p); e=new int[p]; puts("Enter elements of array"); vvod(e,p); break;
case 2: //bubble sort puts("Array A[] sorted by method \"Bubble Sort\":"); bubble_sort(a,l); out(a,l); step=1; file_write(a,l); break;
case 3: //min sort puts("Array B[] sorted by method \"Min element\":"); min_sort(b,m); out(b,m); step=2; file_write(b,m); break;
case 4: //insert sort puts("Array C[] sorted by method \"Insert Sort\":"); insert_sort(c,n); out(c,n); step=3; file_write(c,n); break;
case 5: //Shell sort puts("Array D[] sorted by method \"Shell Sort\":"); shell_sort(d,o); out(d,o); step=4; file_write(d,o); break;
case 6: //Hoare sort puts("Array E[] sorted by method \"Hoare Sort\":"); hoare_sort(e,0,p-1); out(e,p); step=5; file_write(e,p); break;
case 7: //Posledovatel'nyi poisk puts("Enter the searching element:"); scanf("%d",&v); search(a,l,b,m,c,n,d,o,e,p,v); break;
case 8: //Binarnyi poik puts("Enter the searching element:"); scanf("%d",&v); search_b(a,l,b,m,c,n,d,o,e,p,v); break;
default: printf ("Invalid choice.\n"); break; } instructions(); printf("?"); scanf("%d", &choice); }//while printf("End of run.\n"); fclose(file); } /*Вывести инструкции*/ void instructions(void) { printf("\nEnter choice:\n" "1. Initilization arrays.\n" "2. Result of bubble sort.\n" "3. Result of min sort.\n" "4. Result of insert sort.\n" "5. Result of Shell sort.\n" "6. Result of Hoor sort.\n" "Poisk:\n" "7. Posledovatelnyi poisk.\n" "8. Binarnyi poik.\n" "9. Exit.\n\n"); }
void swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; } void vvod(int *a1,int count) { for(i=0;i<count;i++) cin>>*(a1+i); } void out(int *a1,int count) { for(i=0;i<count;i++) cout<<*(a1+i)<<" "; cout<<endl; } void bubble_sort(int *a1,int count) { for (i=0; i<count; i++) for (j=count-1; j>i; j--) if (*(a1+j-1) > *(a1+j)) swap((a1+j-1),(a1+j)); /*поменять их местами */ }
void min_sort(int *a1,int count) { int min_i, temp; for (i = 0; i<count-1; i++) { /* устанавливаем начальное значение минимального индекса */ min_i = i; /* находим минимальный индекс элемента */ for (j =i+1; j<count; j++) { if (*(a1+j) < *(a1+min_i)) { min_i = j; } } /* меняем значения местами */ temp = *(a1+i); *(a1+i) = *(a1+min_i); *(a1+min_i) = temp; }}
void insert_sort(int *a1,int count) { for (i=1; i<=count-1; i++) { j=i; while (*(a1+j)<*(a1+j-1) && j>=1) { swap((a1+j),(a1+j-1)); j--; } } } void shell_sort(int *x, int count) { int i,j; //две переменные цикла int gap; //шаг сортировки int sorted; //флаг окончания этапа сортировки for(gap=count/2; gap>0; gap/=2)//начало сортировки do { sorted = 0; for(i=0,j=gap;j<count;i++,j++) if(*(x+i)>*(x+j)) { swap((x+i),(x+j)); sorted = 1; } }while(sorted); }
void hoare_sort(int *x,int l,int r) { int sr=*(x+(l+r)/2); i=l; j=r; do { while(*(x+i)<sr) i++; while(*(x+j)>sr) j--; if(i<=j) { swap((x+i),(x+j)); i++; j--; } }while(i<=j); if(i<r) hoare_sort(x,i,r); if(j>l) hoare_sort(x,l,j); } void search(int *a, int l,int *b,int m,int *c,int n,int *d,int o,int *e,int p, int v) { int count=0; step=9; fprintf(file,"\n\nPosledovatelnyi poisk:\n"); fprintf(file,"\nA[]:"); file_write(a,l); fprintf(file,"\nB[]:"); file_write(b,m); fprintf(file,"\nC[]:"); file_write(c,n); fprintf(file,"\nD[]:"); file_write(d,o); fprintf(file,"\nE[]:"); file_write(e,p); fprintf(file,"\n"); for (i=0; i<l; i++) //a {if (*(a+i)==v) { fprintf(file,"In A[] element-%d is in position- %d\n",v,(i+1)); count++; } } for (i=0; i<m; i++) //b {if (*(b+i)==v) { fprintf(file,"In B[] element-%d is in position- %d\n",v,(i+1)); count++; } } for (i=0; i<n; i++) //c {if (*(c+i)==v) { fprintf(file,"In C[] element-%d is in position- %d\n",v,(i+1)); count++; } } for (i=0; i<o; i++) //d {if (*(d+i)==v) { fprintf(file,"In D[] element-%d is in position- %d\n",v,(i+1)); count++; } } for (i=0; i<p; i++) //e {if (*(e+i)==v) { fprintf(file,"In E[] element-%d is in position- %d\n",v,(i+1)); count++; } } if(count==0) fprintf(file,"Element-%d ne naiden\n",v);
}
void search_b(int *a, int l,int *b,int m,int *c,int n,int *d,int o,int *e,int p, int v) { //сортировка массивов bubble_sort(a,l); min_sort(b,m); insert_sort(c,n); shell_sort(d,o); hoare_sort(e,0,p-1); //вывод массивов fprintf(file,"\n\nBinarnyi poisk:\n"); step=9; fprintf(file,"\nA[]:"); file_write(a,l); fprintf(file,"\nB[]:"); file_write(b,m); fprintf(file,"\nC[]:"); file_write(c,n); fprintf(file,"\nD[]:"); file_write(d,o); fprintf(file,"\nE[]:"); file_write(e,p); fprintf(file,"\n"); //поиск элемента int count=0; int Left,Right,Medium; //a[] Left=0; Right=l-1; while(Left<=Right) { Medium=(Left+Right)/2; if(a[Medium]==v) { fprintf(file,"In A[] element-%d is in position %d\n",v,(Medium+1)); count++; break; }; if(a[Medium]>v) Right=Medium-1; else Left=Medium+1; } //b[] Left=0; Right=m-1; while(Left<=Right) { Medium=(Left+Right)/2; if(b[Medium]==v) { fprintf(file,"In B[] element-%d is in position %d\n",v,(Medium+1)); count++; break; }; if(b[Medium]>v) Right=Medium-1; else Left=Medium+1; } //c[] Left=0; Right=n-1; while(Left<=Right) { Medium=(Left+Right)/2; if(c[Medium]==v) { fprintf(file,"In C[] element-%d is in position %d\n",v,(Medium+1)); count++; break; }; if(c[Medium]>v) Right=Medium-1; else Left=Medium+1; } //d[] Left=0; Right=o-1; while(Left<=Right) { Medium=(Left+Right)/2; if(d[Medium]==v) { fprintf(file,"In D[] element-%d is in position %d\n",v,(Medium+1)); count++; break; }; if(d[Medium]>v) Right=Medium-1; else Left=Medium+1; } //e[] Left=0; Right=p-1; while(Left<=Right) { Medium=(Left+Right)/2; if(e[Medium]==v) { fprintf(file,"In E[] element-%d is in position %d\n",v,(Medium+1)); count++; break; }; if(e[Medium]>v) Right=Medium-1; else Left=Medium+1; }
if(count==0) fprintf(file,"Element-%d ne naiden\n",v); }
void file_write(int *a1,int count) { if(step==0) fprintf(file,"\t\t RESULTS OF SORTS\n"); if(step==1) fprintf(file,"\n Bubble Sort A[]:\n"); if(step==2) fprintf(file,"\n Min Sort: B[]\n"); if(step==3) fprintf(file,"\n Insert Sort: C[]\n"); if(step==4) fprintf(file,"\n Shell Sort: D[]\n"); if(step==5) fprintf(file,"\n Hoare Sort: E[]\n"); if(step!=0) for(i=0;i<count;i++) fprintf(file,"%d ",*(a1+i)); }