Ejemplo n.º 1
0
//This function is called to begin the insertion sort
//It first copies the original array to the manipulation array and also resets the sortString
string Sorts::InsertSort()
{
	copyArray();					//Copies Original Array to the array that will be sorted
	SortString = "";				//Empty the string

	starttime = (long)time(NULL);	//Record Start time

	ISort();						//Perform the Sort

	endtime = (long)time(NULL);		//Record the endtime
	totalseconds = endtime - starttime;//Calculate seconds elapsed
	minutes = totalseconds / 60;	// seconds in a minute
	seconds = totalseconds % 60;	// the rest are the seconds

	//-----The code below appends necessary data to the SortString
	SortString.append("Sort Type: InsertSort()\n");
	SortString.append("Number Sorted : ");
	SortString.append(to_string(n-1));
	SortString.append("\n");

	SortString.append("End Time      : ");
	SortString.append(to_string(endtime));
	SortString.append("\n");
	
	SortString.append("Start Time    : ");
	SortString.append(to_string(starttime));
	SortString.append("\n");

	SortString.append("Elapsed Time  : ");
	SortString.append(to_string(minutes));
	SortString.append(" Minutes, ");
	SortString.append(to_string(seconds));
	SortString.append(" Seconds");
	//------End sortString appendage code

	IsortPrint();//prints sortString to the ISSorted.txt file

	return SortString;
}
Ejemplo n.º 2
0
SMatrix ReadSparse(char *name, char *probName)
{
	FILE *fp;
	long n, m, i, j;
	long n_rows, tmp;
	long numer_lines;
	long colnum, colsize, rownum, rowsize;
	char buf[100], type[4];
	SMatrix M, F;

	if (!name || name[0] == 0) {
		fp = stdin;
	} else {
		fp = fopen(name, "r");
	}

	if (!fp) {
		Error("Error opening file\n");
	}

	fscanf(fp, "%72c", buf);

	fscanf(fp, "%8c", probName);
	probName[8] = 0;
	DumpLine(fp);

	for (i=0; i<5; i++) {
	  fscanf(fp, "%14c", buf);
	  sscanf(buf, "%ld", &tmp);
	  if (i == 3)
	    numer_lines = tmp;
	}
	DumpLine(fp);

	fscanf(fp, "%3c", type);
	type[3] = 0;
	if (!(type[0] != 'C' && type[1] == 'S' && type[2] == 'A')) {
	  fprintf(stderr, "Wrong type: %s\n", type);
	  exit(0);
	}

	fscanf(fp, "%11c", buf); /* pad */

	fscanf(fp, "%14c", buf); sscanf(buf, "%ld", &n_rows);

	fscanf(fp, "%14c", buf); sscanf(buf, "%ld", &n);

	fscanf(fp, "%14c", buf); sscanf(buf, "%ld", &m);

	fscanf(fp, "%14c", buf); sscanf(buf, "%ld", &tmp);
	if (tmp != 0)
	  printf("This is not an assembled matrix!\n");
	if (n_rows != n)
	  printf("Matrix is not symmetric\n");
	DumpLine(fp);

	fscanf(fp, "%16c", buf);
	ParseIntFormat(buf, &colnum, &colsize);
	fscanf(fp, "%16c", buf);
	ParseIntFormat(buf, &rownum, &rowsize);
	fscanf(fp, "%20c", buf);
	fscanf(fp, "%20c", buf);
		
	DumpLine(fp); /* format statement */

	M = NewMatrix(n, m, 0);

	ReadVector(fp, n+1, M.col, colnum, colsize);

	ReadVector(fp, m, M.row, rownum, rowsize);

	for (i=0; i<numer_lines; i++) /* dump numeric values */
	  DumpLine(fp);

	for (i=0; i<n; i++)
		ISort(M, i);

	for (i=0; i<=n; i++)
	  M.startrow[i] = M.col[i];

	fclose(fp);

	F = LowerToFull(M);

	maxm = 0;
	for (i=0; i<n; i++)
	  if (F.col[i+1]-F.col[i] > maxm)
	    maxm = F.col[i+1]-F.col[i];

	if (F.nz) {
	  for (j=0; j<n; j++)
	    for (i=F.col[j]; i<F.col[j+1]; i++)
	      F.nz[i] = Value(F.row[i], j);
	}

	FreeMatrix(M);

	return(F);
}