Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Различные методы сортировки и поиска си, программирование .docx
Скачиваний:
8
Добавлен:
04.01.2017
Размер:
49.33 Кб
Скачать

Сортировка с помощью дерева

#include <stdio.h>

#include <windows.h>

void PRINT(char str[]) {

char buf[200];

CharToOem(str,buf);

printf("%s ",buf); }

typedef struct tree {

int a;

struct tree *left;

struct tree *right;

} TREE;

TREE *add_to_tree(TREE *root, int new_value){

if (root==NULL) {

root = (TREE*)malloc(sizeof(TREE));

root->a = new_value;

root->left = root->right = 0;

return root; }

if (root->a < new_value)

root->right = add_to_tree(root->right, new_value);

else

root->left = add_to_tree(root->left, new_value);

return root; }

void tree_to_array(TREE *root, int a[]) {

static max2=0;

if (root==NULL) return;

tree_to_array(root->left, a);

a[max2++] = root->a;

tree_to_array(root->right, a);

free(root); }

void sort_tree(int a[], int elem_total) {

TREE *root;

int i;

root = NULL;

for (i=0;i<elem_total;i++) root = add_to_tree(root, a[i]);

tree_to_array(root, a);}

void main(void) {

FILE* file_from, *file_to;

int *arr;

int fend,i=0,j;

char in_name[40],out_name[40];

PRINT("Имя файла данных: ");

scanf("%s",&in_name);

PRINT("Имя выходного файла: ");

scanf("%s",&out_name);

file_from = fopen(in_name,"r");

file_to = fopen(out_name,"w+");

if (file_from != NULL) {

fseek(file_from,0,SEEK_END);

fend = ftell(file_from);

arr = (int*)malloc(fend*sizeof(int));

fseek(file_from,0,SEEK_SET);

PRINT("Данные из файла");

printf("%s",in_name);

PRINT(" до сортировки:\n");

while (ftell(file_from) < fend) {

fscanf(file_from,"%d",&arr[i]);

printf("%d ",arr[i]);

i++; }

PRINT("\n\nДанные после сортировки, которые будут записаны в файл");

printf("%s:\n",out_name);

sort_tree(arr,i);

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

fprintf(file_to,"%d ",arr[j]);

printf("%d ",arr[j]); }

}else PRINT("Ошибка. Нет такого файла данных!!!\n");

printf("\n\n");

system("pause"); }

Хеширование

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <locale.h>

#include <time.h>

struct dann{

int key;

float mas[5];

char str[20];

}; struct dann* vvod_dann(int *N) {

int i;

struct dann *A;

printf("Введите количество элементов в информационном массиве: ");

scanf("%d",N);

A=(struct dann*)malloc(*N*sizeof(struct dann));

srand((unsigned)time(NULL));

for(i=0;i<*N;i++) {

for(int j=0;j<5;j++)

A[i].mas[j]=(float)rand()/100;

printf("Введите ключ %d: ",i);

scanf("%d", &A[i].key);

printf("Введите строку %d: ",i);

scanf("%s", &A[i].str); }

return A; }

int** hesh(struct dann *A,int N) { int **hesh_tabl,p;

hesh_tabl=(int**)malloc(1000*sizeof(int*));

for(int i=0;i<1000;i++) {

hesh_tabl[i]=(int*)malloc(2*sizeof(int));

hesh_tabl[i][0]=0;

hesh_tabl[i][1]=0; }

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

p=A[i].key%1000;

if(hesh_tabl[p][0]==0) {

hesh_tabl[p][0]=A[i].key;

hesh_tabl[p][1]=i; }

else {

while((hesh_tabl[p][0])!=0)

p++;

hesh_tabl[p][0]=A[i].key;hesh_tabl[p][1]=i; } }

return hesh_tabl; }

void find(struct dann *A, int **tabl, int N) {

int key,p=0,k;

printf("Введите искомое значение: ");

scanf("%d",&key);

k=key%1000;

if(tabl[k][0]==key)

p=1;

else {

while(tabl[k][0]!=key)

k++;

p=1; }

if(p==1) {

for(int i=0;i<5;i++)

printf("\nInf_mas[%d].mas[%d]=%f",tabl[k][1],i,A[tabl[k][1]].mas[i]);

printf("\n%s",A[tabl[k][1]].str); } }

void main() {

setlocale(LC_ALL,"Rus");

int **hesh_tabl,N,i,j;

struct dann *Inf_mas;

Inf_mas=vvod_dann(&N);

hesh_tabl=hesh(Inf_mas,N);

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

for(j=0;j<5;j++)

printf("%f *|* ",Inf_mas[i].mas[j]);

printf("\nInf_mas[%d].key= %d\n\n",i,Inf_mas[i].key);}find(Inf_mas,hesh_tabl,N);

getch(); }