Example #1
0
void main()
{
    int arr[] = { 8, 4, 6, 9, 2, 3, 1 };

    Selection_Sort(arr, sizeof(arr)/sizeof(int));

    for (int i = 0; i < 7; i++)
        printf("%d ", arr[i]);
}
void Test_Selection_Sort()
{
    int nArrary[50];

    assert(GetRandArray(nArrary, sizeof(nArrary)/sizeof(int)));
    DumpArray(nArrary, sizeof(nArrary)/sizeof(int));
    assert(Selection_Sort(nArrary, sizeof(nArrary)/sizeof(int)));
    DumpArray(nArrary, sizeof(nArrary)/sizeof(int));
}
Example #3
0
int main(int argc, char *argv[])
{
    long *Data;
    long size;
    long i,j,n,seed;
    FILE *fp;
    char  *filename;
    
    if(argc != 2)
    {
        printf("Please Input like this.\n");
        printf("./a.out 10000\n");
        return 0;
    }
    
    if(argc <0){
        printf("Please Input more than 0\n");
        printf("\n");
        return 0;
        
    }
    
    
    /*  alocation  */
    char *ends;
    size = strtol(argv[1], &ends,10);
    
    Data = (long*)malloc(sizeof(long)* size);
    if (Data == NULL) {
        printf( "memory allocation error\n" );
        exit(EXIT_FAILURE);
    }
   
    /* Make Data */
    srand((unsigned int)time(NULL)); //Hiroshi seedの工夫
    for(i=0;i<size;i++) Data[i]=rand();
    
#if DEBUG == 1
    /*Write data before sorting*/
    filename = "before_selection_sort" ;
    fp = fopen(filename, "w");
    for(i=0;i<size;i++)  fprintf( fp, "%ld\n",   Data[i]  );
    fclose(fp);
#endif
    
    COMPARE_COUNT=0;
    clock_t start, end;
    start = clock();
    Selection_Sort(Data,size);
    end = clock();
    printf("Selection Sort=\t\tComparing: %ld\n" , COMPARE_COUNT);
    printf("time: %f[sec]\n", (double)(end - start) / CLOCKS_PER_SEC);

#if DEBUG == 1
    /*Write data after sorting*/
    filename = "after_selection_sort" ;
    fp = fopen(filename, "w");
    for(i=0;i<size;i++) fprintf( fp, "%ld\n",   Data[i]  );
    fclose(fp);
#endif

    /* free memory */
    free(Data); 
    return 0;

}