Cod sursa(job #2078777)

Utilizator jurjstyleJurj Andrei jurjstyle Data 29 noiembrie 2017 22:28:13
Problema Sortare prin comparare Scor 40
Compilator c Status done
Runda Arhiva educationala Marime 3.14 kb
#include <stdio.h>
#include <stdlib.h>
#include <assert.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[Right];
	int IndexLeft = Left - 1;

	for (int Counter = Left; Counter <= Right - 1; ++Counter)
	{
		if (Vector->Data[Counter] <= Pivot)
		{
			IndexLeft += 1;
			Swap(&Vector->Data[IndexLeft], &Vector->Data[Counter]);
		}
	}

	Swap(&Vector->Data[IndexLeft + 1], &Vector->Data[Right]);
	return (IndexLeft + 1);
}

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

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

int VecSort(CC_VECTOR *Vector)
{
	//we will use QuickSort method
	QuickSort(Vector, 0, Vector->Count - 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()
{
    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;
}