Esempio n. 1
0
int main (int argc, char ** argv)
{
	if (argc != 5)
	{
		printf("\nInvalid number of arguments");
		return EXIT_FAILURE;
	}

	//Assign inputs to variables
	char * mode = argv[1];
	char * in_file = argv[2];
	char * seq_file = argv[3];
	char * out_file = argv[4];

	int Size = 0;	
	long * arr;

	double N_Comp = 0;
	double N_Move = 0;

	clock_t sort_t = 0; //keeps track of sort time
	clock_t io_sum = 0; //keeps track of total I/O time
	
	clock_t io = clock();
	arr = Load_File(in_file, &Size); //Load the fille and store the contents in arr
	io_sum = clock() - io;

	if (*mode == 'i')
	{	
		sort_t = clock();
		Shell_Insertion_Sort(arr, Size, &N_Comp, &N_Move);
		sort_t = clock() - sort_t;
	}
	else if (*mode == 's')
	{
		sort_t = clock();
		Shell_Selection_Sort(arr, Size, &N_Comp, &N_Move);
		sort_t = clock() - sort_t;
	}
	else
	{
		printf("\nInvalid argument for sort type");
		return EXIT_FAILURE;
	}

	io = clock();
	Print_Seq(seq_file, Size);	
	Save_File(out_file, arr, Size);
	io_sum += clock() - io;
	
	free(arr);

	printf("Number of comparisons: %le\n", N_Comp);
	printf("Number of moves: %le\n", N_Move);
	printf("I/O Time: %le\n", ((double)io_sum) / CLOCKS_PER_SEC);
	printf("Sorting Time: %le\n", ((double)sort_t) / CLOCKS_PER_SEC);

	return EXIT_SUCCESS;
}
Esempio n. 2
0
int main (int argc, char ** argv)
{
  if (argc != 5)
  {
    printf("There should be four arguments!\n");
    printf("the sorting algorithm, input file, sequence file, and output file\n");
    return EXIT_FAILURE;
  }
  int size = 0;
  int check = 0; 
  double * n_comp = 0;
  double * n_move = 0;
  long * array = Fileload(argv[2], &size);
  if (array == NULL)
  {
    printf("File could not be opened\n");
    return EXIT_FAILURE;
  }
  
  
  if (*argv[1] == 'i')
  {
    Shell_Insertion_Sort(array, size, n_comp, n_move);
  }
  
  if (*argv[1] == 's')
  {
    Shell_Selection_Sort(array, size, n_comp, n_move);
  }
  
  
  check = Filesave(argv[4], array, size);
  if (check == 0)
  {
    printf("File did not save properly\n");
    free(array);
    return EXIT_FAILURE;
  }
  free(array);
  return EXIT_SUCCESS;

}
Esempio n. 3
0
int main(int argc, char * argv[])
{
  // Check to make sure the correct number of input arguments are provided
  if(argc != 5)
  {
    printf("Usage: ./proj1 <sorting_method> <input_file> <sequence_file> <output_file>\n");
    return EXIT_FAILURE;
  }

  // Set these variables to the input arguments (Make things easier)
  char method = * argv[1];
  char * input_file = argv[2];
  char * sequence_file = argv[3];
  char * output_file = argv[4];

  // Used to store values used in this program
  int Size;
  long * values;
  clock_t t_sort, t_load, t_save, t_seq;

  // Load the file, keeping track of the time it takes to load the file
  t_load = clock();
  values = Load_File(input_file, &Size);
  t_load = clock() - t_load;

  // Check to make sure the files was loaded correctly
  if(values == NULL)
  {
    printf("Error loading the input file\n");
    return EXIT_FAILURE;
  }


  // Print the sequence to a file, keeping track of the time it takes to write to the file
  t_seq = clock();
  int i = Print_Seq(sequence_file, Size);
  t_seq = clock() - t_seq;
  
  // Check to make sure the sequence was correctly output to the file
  if(i == 0)
  {
    printf("Failed to print sequence\n");
    free(values);
    return EXIT_FAILURE;
  }

  // Used to keep track of the number of moves and comparisons in the sorting algorithms
  double N_Comp = 0.0, N_Move = 0.0;


  // Check for the sorting method chosen by the user
  // Keep track of the time it takes to sort the values as well
  t_sort = clock();
  if(method == 'i')
  {
    Shell_Insertion_Sort(values, Size, &N_Comp, &N_Move);
  }
  else if(method == 's')
  {
    Shell_Selection_Sort(values, Size, &N_Comp, &N_Move);
  }
  else
  {
    printf("Sorting method must be either i (Insertion) or s (Selection) sort\n");
    free(values);
    return EXIT_FAILURE;
  }
  t_sort = clock() - t_sort;


  // Save the sorted array to the output file, keeping track of th time it takes as well
  t_save = clock();
  int returned = Save_File(output_file, values, Size);
  t_save = clock() - t_save;

  // Check to make sure that the file was correctly output to
  if (!returned)
  {
    printf("Failed to output values to file\n");
    free(values);
    return EXIT_FAILURE;
  }
  else if(returned != Size)
  {
    printf("Number of values returned not equal to number of values specified\n");
    free(values);
    return EXIT_FAILURE;
  }
  
  // Free the malloced values
  free(values);

  // Display all the required information to the screen
  Screen_Dump(N_Comp, N_Move, (t_save + t_load + t_seq) / CLOCKS_PER_SEC, t_sort);

  // SUCCESS!!!
  return EXIT_SUCCESS;
}