Ejemplo n.º 1
0
Archivo: sorts.c Proyecto: tectrendz/p
int main()
{
   int unsorted = 1, length, sorted;	
   int i[] = {4, 6, 7, 9, 1, 2, 10};
   int key = 0;
   int changed = 0;
   printf("size of arrray is %ld\n", sizeof(i)/sizeof(int)); 
   length = sizeof(i)/sizeof(int);
   for (sorted = 0; sorted < length; sorted++) {
       printf("%d ", i[sorted]);
   }
   printf("\n");

#ifdef IS
   insertion_sort(i, length);
   printf("\nIS\n");
   for (sorted = 0; sorted < length; sorted++) {
       printf("%d ", i[sorted]);
   }
   printf("\n");
#endif

   quickSort(i, 1, length);
   for (sorted = 0; sorted < length; sorted++) {
       printf("%d ", i[sorted]);
   }
   printf("\n");
   qs(i, 0, length-1);
   for (sorted = 0; sorted < length; sorted++) {
       printf("%d ", i[sorted]);
   }
   printf("\n");
   return 1;
}
Ejemplo n.º 2
0
int main(void)
{
	int v[21];
	int n=20;
	count c;

	setlocale (LC_ALL, "Portuguese");

	resetVetor(v, n);
	printf("Vetor usado \n");
	imprimeVetor(v, n);

	printf("selection Sort \n");
	c = selection_sort(v, n);
	automato( c, v, n);

	printf("Selecion Sort v2 \n");
	c = selection_sort2(v, n);
	automato( c, v, n);

	printf("Insertion Sort \n");
	c = insertion_sort(v, n);
	automato( c, v, n);

	printf("Bubble Sort \n");
	c = bubble_sort(v, n);
	automato( c, v, n);
	
	printf("Merge Sort \n");
	c = merge_sort(v, 0, n);
	automato( c, v, n);
	return 0;

}
main() {

    clock_t tempo1;

	//Contagem de tempo do BubbleSort
	cria_vetor();
	tempo1 = clock();
  bubble_sort();
	tempo1 = clock() - tempo1;
  printf("\nBubbleSorte: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do InsertionSort
  cria_vetor();
  tempo1 = clock();
  insertion_sort();
	tempo1 = clock() - tempo1;
  printf("InsertionSort: %.4f seg\n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do SelectionSort
  cria_vetor();
  tempo1 = clock();
  selection_sort();
	tempo1 = clock() - tempo1;
  printf("SelectionSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do Heapsort
  cria_vetor();
  tempo1 = clock();
  heapsort();
	tempo1 = clock() - tempo1;
  printf("HeapSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do MergeSort
  cria_vetor();
	tempo1 = clock();
  mergesort(0, TAM - 1);
	tempo1 = clock() - tempo1;
  printf("mergeSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do QuickSort
  cria_vetor();
	tempo1 = clock();
  quicksort(0, TAM - 1);
	tempo1 = clock() - tempo1;
  printf("QuickSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

	getch();
}
Ejemplo n.º 4
0
// ----------------------------------------------------------------------
// hud_init_escort_info()
//
// Set up the escort list
//
void hud_setup_escort_list(int level)
{
	int num_escorts, num_complete_escorts;
	escort_info complete_escorts[MAX_COMPLETE_ESCORT_LIST];

	hud_escort_clear_all();

	// get complete escort list
	hud_create_complete_escort_list(complete_escorts, &num_complete_escorts);

	// sort escort list by priority
	insertion_sort(complete_escorts, num_complete_escorts, sizeof(escort_info), escort_compare_func);

	// set number in escort list
	num_escorts = num_complete_escorts;
	if (num_escorts > Max_escort_ships) {
		num_escorts = Max_escort_ships;
	}

	// add ships to escort list
	for (Num_escort_ships=0; Num_escort_ships<num_escorts; Num_escort_ships++) {
		Escort_ships[Num_escort_ships].obj_signature = complete_escorts[Num_escort_ships].obj_signature;
		Escort_ships[Num_escort_ships].priority = complete_escorts[Num_escort_ships].priority;
		Escort_ships[Num_escort_ships].objnum = complete_escorts[Num_escort_ships].objnum;
		Escort_ships[Num_escort_ships].np_id = complete_escorts[Num_escort_ships].np_id;
		Escort_ships[Num_escort_ships].escort_hit_timer = 0;
		Escort_ships[Num_escort_ships].escort_hit_next_flash = 0;
		Escort_ships[Num_escort_ships].escort_show_bright = false;
	}

	if(level){
		Show_escort_view = 1;
	}
}
void imprime_nome_voo(List_r lista_de_reservas, List_c lista_de_clientes, List_v voo_actual) {
    int i = 0;
    int n_clientes = 50 - voo_actual->voo.n_lugares_vagos;
    int lista_clientes_reserva[ n_clientes];
    List_r l = lista_de_reservas->next;
    if (!l) {
        printf("\n   Listagem nao efectuada. Reservas nao existentes.\n   ");
        return;
    }
    while (l) {
        if (l->reserva.n_voo == voo_actual->voo.n_voo) {
            lista_clientes_reserva[ i] = l->reserva.n_cliente;
            i++;
        }
        l = l->next;
    }
    i = 0;
    Cliente * lista_clientes[ n_clientes];
    for (i; i < n_clientes; i++) {
        lista_clientes[ i] = &(pesquisa_cliente_numero_cliente(lista_de_clientes, lista_clientes_reserva[i])->cliente);
    }

    insertion_sort(lista_clientes, n_clientes);
    i = 0;
    if (n_clientes > 0) printf("\n\n   Passageiros do voo numero %d:\n", voo_actual->voo.n_voo);
    if (n_clientes == 0) printf("\n\n   Listagem nao efectuada. Passageiros nao existentes.\n");
    for (i; i < n_clientes; i++) {
        printf("   ---------------------------------------------");
        printf("\n\n   Nome do cliente: %s\n", (*lista_clientes[ i]).nome);
        printf("   Numero do cliente: %d\n", (*lista_clientes[ i]).n_cliente);

    }
}
Ejemplo n.º 6
0
int main()
{
    int select, size, *test;
    while (select = menu())
    {
        size = get_integer("Array size");
        while(size <= 0)
            size = get_integer("Array size must bigger then zero");

        test = init_array(test, size);

        printf("Before sort:\n");
        print_array(test, size);
        switch (select) {
            case 1:
                insertion_sort(test,size);
                break;
            case 2:
                shell_sort(test, size);
                break;
            case 3:
                heap_sort(test, size);
                break;
            case 4:
                merge_sort(test, size);
                break;
            case 5:
                quick_sort(test, size);
                break;
        }
        printf("After sort:\n");
        print_array(test, size);
    }
   return 0;
}
Ejemplo n.º 7
0
int main()
{
	int arr[] = {6,5,3,1,8,7,2,4};
	insertion_sort(arr, sizeof(arr)/sizeof(int));
	print_arr(arr, sizeof(arr)/sizeof(int));
	return 0;
}
int main() {
    
    int i=0, cases, median=0, total, sum;
    int relatives[500];
    
    scanf("%d", &cases);
    
    while (cases--) {
        
        scanf("%d", &total);
      
        for (i=0; i<total; i++)
            scanf("%d", &relatives[i]);
            
        insertion_sort(relatives, total);
        
        /* location of Vito's house is the median */
        if (total % 2 == 0)
            median = (relatives[total/2]+relatives[total/2-1])/2;
        else
            median = relatives[total/2];
        
        /* sum of total distance from Vito to everyone else */
        sum = 0;
        for (i=0; i<total; i++)
            sum += abs(relatives[i]-median);
        
        printf("%d\n", sum);
    }
    return 0;
}
Ejemplo n.º 9
0
//The main function will read a text file, copy the data
//int an array and display the time taken to sort the data
int main(int argc, char *argv[]){
  std::random_device rd;
  std::mt19937 gen(rd());
  int N = 50;
  std::uniform_real_distribution<> dis(1, 2000);
  std::vector<double> numbers;
  std::vector<double> numbers_aux;
  for (int n=0; n<N; ++n){
    numbers.emplace_back(dis(gen));
  }
  numbers_aux = numbers;
  selection_sort(numbers, N);
  printData<double>(numbers, "Selection Sort");
  numbers = numbers_aux;
  insertion_sort(numbers, N);
  printData<double>(numbers, "Insertion Sort");
  sort_clock  trackTime;
  trackTime._start = std::chrono::system_clock::now();
  numbers = numbers_aux;
  topdown_merge_sort(numbers, 0, N-1);
  trackTime._end = std::chrono::system_clock::now();
  trackTime._elapsed_seconds = trackTime._end-trackTime._start;
  std::cout << "Finished Merge Top Sort in:" << trackTime._elapsed_seconds.count() << std::endl;
  printData<double>(numbers, "Merge Sort");
  numbers = numbers_aux;
  bottomup_merge(numbers, 0, N);
  printData<double>(numbers, "Merge Sort Bottom Up Sort");
}
Ejemplo n.º 10
0
int is_insertion(int *A,int *B,int n)
{
    int i,j;
    int ret=0;
    for(i=1; i<n+1; i++) //i从2开始,至少有2个元素才需要进行排序。
    {
        int flag=0;
        insertion_sort(A,i);

        for(j=0; j<n; j++)
        {
            if(A[j]!=B[j])
            {
                flag=1;
                break;
            }
        }
        if(flag!=1)
        {
            ret=i-1;
            return ret;
        }
    }
    return ret;
}
Ejemplo n.º 11
0
void boost_run_length(sort_state state, run *run){
  // Need to make sure we don't overshoot the end of the array
  int length = state->length - (run->index - state->array);
  if(length > MIN_RUN_SIZE) length = MIN_RUN_SIZE;

  insertion_sort(run->index, length);
  run->length = length;
}
Ejemplo n.º 12
0
int main(int argc, char* argv[])
{
        int a[3] = {31,27,27};

        insertion_sort(a,3);

	return 0;
}
Ejemplo n.º 13
0
int main()
{
  std::vector<int> vec = {1,6,2,4,5,2,6,2};
  insertion_sort(vec);
  for (auto i : vec) std::cout<<i<<" ";

  return 0;
}
Ejemplo n.º 14
0
void unit_tests() {
  int a[4] = {1, 2, 3, 4};
  insertion_sort(a, 4);
  test_sorted(a, 4);

  int b[4] = {10, 20, 40, 30};
  insertion_sort(b, 4);
  test_sorted(b, 4);

  int c[4] = {10, 4000, 20, 300};
  insertion_sort(c, 4);
  test_sorted(c, 4);

  int d[4] = {3000, 40000, 20000, 10000};
  insertion_sort(d, 4);
  test_sorted(d, 4);
}
Ejemplo n.º 15
0
int main()
{
    int arr[] = {7, 2, 0, 1, 8, 3};
    int arr_size = sizeof(arr)/sizeof(int);
    print_arr(arr, arr_size);
    insertion_sort(arr, arr_size);
    insertion_sort_desc(arr, arr_size);
}
Ejemplo n.º 16
0
int main(void)
{
    int nums[7] = {3, 1, 6, 3, 45, 6, 7};
    insertion_sort(nums, 7);
    for (int i = 0; i < 7; i++) {
        printf("%d\n", nums[i]);
    }
}
int main(void)
{
  int array[] = {402, 94111, 98, 12, 1024};

  print_array(array, 5);
  insertion_sort(array, 5);
  print_array(array, 5);
  return (0);
}
Ejemplo n.º 18
0
main()
{
		int arr[8]={10,15,3,25,8,2,19,30};
		//int arr[5]={1,2,3,4,5};

		insertion_sort(arr,8);
		printf("Printing sorted array\n");
		print_array(arr,8);
}
Ejemplo n.º 19
0
void evidence_insertion_sort(void) {
    int* array = occurrences("senselessness");
    huff_list* list = huff_list_builder(array);
    huff_list* sorted_list = insertion_sort(list);
    huff_list_print(sorted_list);
    free(array);
    free(sorted_list);
    return;
}
Ejemplo n.º 20
0
int main()
{
  int array[] = {3, 2, 1, 4, 5, 8, 7, 6};
  int array_length = sizeof(array) / sizeof(array_length);
  print_array(array, array_length);
  insertion_sort(array, array_length);
  print_array(array, array_length);
  return 0;
}
Ejemplo n.º 21
0
int main(int argc, char *argv[]) {
    char option = '\0';
    char *filepath = NULL;
    unsigned int length = 0;
    int *array = NULL;
    int *original_array = NULL;
	struct sorting_stats ops;
    /* parse the filepath given in command line arguments */
    filepath = parse_filepath(argc, argv);

    /* parse the array given in the input file */
    array = array_from_file(filepath, &length);

    /* save a copy of array used to make some checks later */
    original_array = array_duplicate(array, length);

    /* print a simple menu and do the actual sorting */
    do {
        option = print_menu();
        switch (option) {
        case INSERTION_SORT:
            ops = insertion_sort(array, length);
            break;
        case SELECTION_SORT:
            ops = selection_sort(array, length);
            break;
        case QUICK_SORT:
            ops = quick_sort(array, length);
            break;
        case EXIT:
            printf("Exiting.\n");
            return (EXIT_SUCCESS);
        default:
            printf("\n\"%c\" is invalid. Please choose a valid option."
                   "\n\n", option);
        }
    } while (!is_valid_option(option));

    /* show the ordered array in the screen */
    array_dump(array, length);
    
    // show the comparation of the algorithm
    printf("Comparisons: %d \n",(int)ops.comps);
    printf("Swaps: %d \n",(int)ops.swaps);
    
    /* check if it is sorted */
    assert(array_is_sorted(array, length));

    /* check if it is a permutation of original */
    assert(array_is_permutation_of(array, original_array, length));

    /* destroy array */
    array_destroy(array);
    array_destroy(original_array);

    return (EXIT_SUCCESS);
}
Ejemplo n.º 22
0
int main(){
    int A[] = { 31, 41, 59, 26, 41, 58 };
    size_t len = sizeof(A) / sizeof(A[0]);

    insertion_sort(A, len);
    printl(A, len);

    return 0;
}
static void seqquick(T *low, T *high) {
	while (high - low > INSERTION_SIZE) {
		T *p = seqpart(low, high);
		seqquick(low, p);
		low = p + 1;
	}
	
	insertion_sort(low, high);
}
int main(void)
{
  int array[] = {-0, -1};

  print_array(array, 2);
  insertion_sort(array, 2);
  print_array(array, 2);
  return (0);
}
Ejemplo n.º 25
0
int main(){
	int i,k,n,arr[50];
	printf("Enter the value of n : \t");
	scanf("%d",&n);
	printf("Enter the array : ");
	read_array(arr,n);	
	insertion_sort(arr,n);
	display_array(arr,n);
}
Ejemplo n.º 26
0
/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm, insertion sort
    insertion_sort(values,n);
    for(int i = 0;i < n;i++)
    {
        printf("%d ",values[i]);
    }
    return;
}
Ejemplo n.º 27
0
void boost_sublist(sort_stack stack, Sublist *sublist)
{
    int length = stack->length - (sublist->index - stack->list);
    if(length > MIN_sublist_SIZE)
    {
        length = MIN_sublist_SIZE;
    }
    insertion_sort(sublist->index, length);
    sublist->length = length;
}
Ejemplo n.º 28
0
int main(void)
{
	int array[] = {10, 8, 6, 4, 2, 1, 3, 5, 7, 9};

	print_array(array, 10);
	insertion_sort(array, 10);
	print_array(array, 10);

	return 1;
}
Ejemplo n.º 29
0
int main(){
	int arr[]={-8, -22 };
	int arr_size=1;

	insertion_sort( arr, arr_size );
	print_array( arr, arr_size );

return 0;

}
Ejemplo n.º 30
0
int main(int argc, char const *argv[])
{
  
  int array[] = {5,2, 4, 6};

  insertion_sort(array, 4);
  print(array, 4);

  return 0;
}