Sunday, June 11, 2017

cmpt 125 assn 2 q1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void sorted( int arr[], int len)
{
    for(int i =0; i<len; i++){
        arr[i] = i;
    }
}

void reversed (int arr[], int len)
{
    int element = len-1;
    // numbers go backwards
    for(int i =0; i<len; i++){
        arr[i] = element;
        element--;
    }
}

void rando (int arr[], int len)
{
    srand(time(0));
    for(int i=0; i<len; i++){
        arr[i] = rand();
    }
}

void selectionSort( int arr[], int len )
{
        int temp;
        for( int i =0; i<len-1; i++){
                int smallest = i;
                for( int k=i+1; k<len; k++){
                        if(arr[k]< arr[smallest]){
                                smallest = k;
                        }
                }
                temp = arr[i];
                arr[i] = arr[smallest];
                arr[smallest] = temp;
        }
}

void insertionSort( int arr[], int len)
{
        int temp;
        for( int i=0; i<len; i++){
                temp = arr[i];
                int pos = i;
                while( pos >0 && arr[pos-1] > temp){
                        arr[pos] = arr[pos-1];
                        pos--;
                }
                arr[pos] = temp;
        }
}

int main (void)
{
    int array[80000];
    reversed(array, 80000); //insert array here
//    clock_t begin = clock(); //starts timing the sort
    selectionSort(array, 80000); //array to sort
//    clock_t end = clock();
//    double duration = (double)(end-begin)/CLOCKS_PER_SEC;
//    printf("%f", duration);
    return 0;
}

No comments:

Post a Comment