Esempio n. 1
0
int main() {
    
    // Different sizes to test the sort.
    int sizes[] = {100, 1000, 10000, 20000, 40000, 50000, 100000};
    
    int* values; 
    int i;
    
    // Loop through trying each size.
    for (i=0; i<7; i++) {
        
        // Allocate the space for the array and fill it.
        values = (int*)malloc(sizeof(int)*sizes[i]);    
        randArray(values, sizes[i], MAXVAL);
    
        // Time and sort.
        int start = time(0);
        bubbleSort(values, sizes[i]);
        int end = time(0);
        
        // Print out the time and free the array.
        printf("Sorting %d values took %d seconds.\n", sizes[i], end-start);
        free(values);
    }
    
    system("PAUSE");
    return 0;
}
Esempio n. 2
0
int main() {
     
    int myArray[10], val, answer;
    srand(time(0));
    
    // Fill the array randomly, then sort. 
    randArray(myArray, 10, 100);
    
    // Print out the original contents.
    printf("Here are the values in your array: ");
    printArray(myArray, 10);
    
    bubbleSort(myArray, 10);
        
    // Print out the contents after the sort.
    printf("Here are the values in your array: ");
    printArray(myArray, 10);
    
    // Ask the user for an element for which to search.
    printf("For which element would you like to search?");
    scanf("%d", &val);
    
    // Perform a binary search.
    answer = BinarySearch(myArray, 10, val);
    
    // Print out the results!
    if (answer != NOT_FOUND)
       printf("Your value was found in index %d of the array.\n", answer);
    else
       printf("Your value wasn't found.\n");
        
    system("PAUSE");
    return 0;
}
Esempio n. 3
0
		\nquickSort(), randArray(), and randomized_quickSort():\n");
//************************ PARTITION() *****************************************
// This example will print out the iterations of the partition function as 
// demonstrated in Figure 7.1 on p.172 in our text
	int A[8] = {2,8,7,1,3,5,6,4}; // starting array

	printf("\nFollowing PARTITION() example on p.172 of our text:\n");
	printf("Before partition():\n");
	for (int i = 0; i < 8; i++)
	{
		printf("%d ", A[i]);
	}	
	printf("\n");

	int q = partition(A,0,7);

	printf("\nAfter partition():\n");
	for (int i = 0; i < 8; i++)
	{
		printf("%d ", A[i]);
	}	
	printf("\nwith q = %d\n", q);

//************************ QUICKSORT() *****************************************
	quickSort(A, 0, 7);
	printf("\n...and after quickSort():\n");
	for (int i = 0; i < 8; i++)
	{
		printf("%d ", A[i]);
	}	
	printf("\n");

//************************ randArray() *****************************************
	printf("\nCalling randArray(10) yields this array: \n");
	int* TEST = randArray(10);
	for(int i = 0; i < 10; i++)
	{
		printf("%d ", TEST[i]);
	}
	printf("\n");

//****************** RANDOMIZED-QUICKSORT() ************************************
	randomized_quickSort(TEST, 0, 9);
	printf("\nAfter randomized_quickSort(): \n");
	for(int i = 0; i < 10; i++)
	{
		printf("%d ", TEST[i]);
	}
	printf("\n\n");

	return 0;
}
Esempio n. 4
0
int main()
{
    int *array = randArray(SORTCOUNT, 1000);
    printArray(array, SORTCOUNT, 0);
/*
    bubbleSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    insertSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    shellSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    quickSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    selectionSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
*/
    mergeSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);


    free(array);
    return 0;
}
float task2b(int data[950][10], int startValidation, int nPositive, int nNegative, int nTotal, int nFold){

	FILE *knnFile;
	knnFile= fopen("posterioriKNN", "w");

	int nObjects=950;
	int dMatrix[950][950]; 
	int nAtributes=9;
	int tValidation= nPositive+nNegative;

	//Dimilarity Matrix
	int i;
	for (i = 0; i < nObjects; i++)
	{
        int j;
		for (j = i; j < nObjects; ++j)
		{
			int cont=0;
            int k;
			for (k = 0; k < nAtributes; k++)
			{
				if(data[i][k]!=data[j][k]){
					cont++;
				}
			}		
			dMatrix[i][j]=cont;
			dMatrix[j][i]=cont;
			//printf("Dissimilarity between i:%d and j:%d = %d\n",i, j, dMatrix[i][j]);
		}
		//printf("\n");
	}

	int v=15; //Number of neighbors
	int p; //Xp where p in [1..n]
	int ClasseKnn[nObjects]; //Tabela de resultados
	int Errors=0; //Contagem de error entre classificação a priori e com a método do k-NN vizinhos

	int distP[958][2];
	
	for (int i = 0; i < nObjects; ++i)
	distP[i][0]=1000;

	int res[v][4];

	for (p=startValidation; p<(startValidation+tValidation); p++)
	{

		for (int i = 0; i < nObjects; i++)
		{
			if((i<startValidation || i>=(startValidation+tValidation))&& i!=p){
			distP[i][0]=dMatrix[p][i];
			distP[i][1]=data[i][9];
			}
		}

		//Ordernar a matriz dispP para pegar todos as distâncias menores
		qsort(distP, nObjects, 2*(sizeof(int)),cmpfunc);

		// for (int i = 0; i < nObjects; ++i)
		// {
		//  	printf("%i %i\n", distP[i][0], distP[i][1]);
		// }

		int check=1;
		int v2=v-1;
		while(check){
			if(distP[v2][0]!=distP[v2+1][0]){
				check=0;
			}else{
				v2++;
			}
		}

		check=1;
		int v3=v-1;
		while(check){
			if((distP[v3][0]!=distP[v3+1][0])|| v3==0){
				check=0;
			}else{
				v3--;
			}
		}

		//Trick to make things right!!!
		if(v3!=0){
			v3++;
		}

		int auxArray[v2-v3+1];
		int j=v3;
		for (int i = 0 ; i <v2-v3+1 ; i++, j++)
		{
			auxArray[i]=j;
		}
		randArray(auxArray, v2-v3+1);

		//Counting of results
	    int count1=0, count2=0;

	    for (i=0; i<v3; i++)
	        if (distP[i][1]==1)
	            count1++;
	        else
	            count2++;

	    int k=0;
	   	for (int i = v3; i < v; i++,k++)
	   	{
	   		if (distP[auxArray[k]][1]==1)
	            count1++;
	        else
	            count2++;
	   	}


	   // printf("count1=%d, count2=%d \n", count1, count2);

	    if (count1>count2)
	        ClasseKnn[p]=1;
	    else ClasseKnn[p]=2;

	    //Posteriori probability
	    float P1=(float)count1/(float)v;
	    float P2=(float)count2/(float)v;

	    fprintf(knnFile, "%f %f\n",P1, P2);

	  //  printf("p=%d na classe %d", p, ClasseKnn[p]);

	    if (ClasseKnn[p]!=data[p][9])
	        Errors++;
}
	    
	 fclose(knnFile);
	//printf("Percentage of errors between classification K-NN and real classification = %f\n", (float)Errors/(float)tValidation);

	return ((float)Errors/(float)tValidation);
}
Esempio n. 6
0
void CCardOperator::shuffle(){
    randArray(cardArray, 52);
}