Cod sursa(job #2087539)

Utilizator jurjstyleJurj Andrei jurjstyle Data 13 decembrie 2017 20:01:24
Problema Sortare prin comparare Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 3.59 kb
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

typedef struct _CC_VECTOR {
    // Members
    int Size;
    int Count;
    int *Data;

} CC_VECTOR;


int VecReAlloc(CC_VECTOR *Vector)
{
    //When our Vector doesn't have a sufficient size, we reallocate while doubling the memory (to maintain efficiency)
    //If we would have resizing with +1 everytime, we would have an overall N^2 complexity just for this operation

    //we double the vector Size
    Vector->Size = Vector->Size * 2;

    //we reallocate the memory
    Vector->Data = (int *)realloc(Vector->Data, Vector->Size * sizeof(int));

    //check if we could reallocate the memory
    if (Vector->Data == NULL)
        return -1;

    //Success
    return 0;
}

void Swap(int * A, int * B)
{
    int tmp = *A;
    *A = *B;
    *B = tmp;
}

int VecCreate(CC_VECTOR **Vector)
{
    //we allocate memory for a member of type CC_VECTOR
    *Vector = (CC_VECTOR *)malloc(sizeof(CC_VECTOR));
    //if the allocation failed, return -1
    if (*Vector == NULL)
        return -1;
    //We initialize the allocated member : size = 1, count = 0 and one int for the actual vector
    (*Vector)->Count = 0;
    (*Vector)->Size = 1;
    (*Vector)->Data = (int *)malloc((*Vector)->Size * sizeof(int));

    //if the allocation failed for the actual vector, return -1
    if ((*Vector)->Data == NULL)
        return -1;

    //Success
    return 0;
}

int VecInsertTail(CC_VECTOR *Vector, int Value)
{
    //we check first if we need to extend the vector
    if (Vector->Count == Vector->Size)
    {
        int Alloc = VecReAlloc(Vector);
        //if it failed , the insert failed
        if (Alloc == -1)
            return -1;
    }
    //our vector start from 0 .. it has n elements : 0,.., n - 1. So we add on position n(Vector -> Count)
    Vector->Data[Vector->Count] = Value;
    //then we increment the number of elements
    Vector->Count += 1;

    //just a check
    if (Vector->Data[Vector->Count - 1] != Value)
        return -2;

    //Success
    return 0;
}

int GetPartition(CC_VECTOR * Vector, int Left, int Right)
{
    int Pivot = Vector->Data[Left + rand() % (Right - Left + 1)];
    int IndexLeft = Left - 1;
    int IndexRight = Right + 1;
    int ok = 1;

    while (ok)
    {
        do
        {
            IndexLeft += 1;
        }while(Vector->Data[IndexLeft] < Pivot);

        do
        {
            IndexRight -= 1;
        }while(Vector->Data[IndexRight] > Pivot);

        if (IndexLeft >= IndexRight)
            return IndexRight;
        Swap(&Vector->Data[IndexLeft], &Vector->Data[IndexRight]);
    }
}

void QuickSort(CC_VECTOR *Vector, int Left, int Right)
{
    if (Left < Right)
    {
        int Partition = GetPartition(Vector, Left, Right);

        QuickSort(Vector, Left, Partition);
        QuickSort(Vector, Partition + 1, Right);
    }
}

int VecSort(CC_VECTOR *Vector)
{
    //we will use QuickSort method
    int length = Vector->Count;
    QuickSort(Vector, 0, length - 1);

    return 0;
}

void print_vector(CC_VECTOR * Vector, int n)
{
    freopen("algsort.out","w",stdout);
    for (int i = 0; i < n; ++i)
        printf("%d ", Vector->Data[i]);
}


CC_VECTOR *V;



int main()
{
    srand(time(NULL));
    freopen("algsort.in", "r", stdin);
    int n;
    scanf("%d", &n);
    VecCreate(&V);
    for(int i = 0; i < n; ++i)
    {
        int x;
        scanf("%d", &x);
        VecInsertTail(V, x);
    }
    VecSort(V);
    print_vector(V, V -> Count);


    return 0;
}