Cod sursa(job #3180813)

Utilizator razviOKPopan Razvan Calin razviOK Data 6 decembrie 2023 00:00:57
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.85 kb
#include <stdio.h>
#include <stdlib.h>

void Merge(int *arr,int low,int mid,int high){

    int numA=mid-low+1;
    int numB=high-mid;
    int numC=high-low+1;
    int *A=(int *) malloc(sizeof(int)*numA);
    int *B=(int *) malloc(sizeof(int)*numB);
    int *C=(int *) malloc(sizeof(int)*numC);

    for(int i=0;i<numA;i++)
        A[i]=arr[low+i];

    for(int j=0;j<numB;j++)
        B[j]=arr[mid+1+j];

    int i=0;
    int j=0;
    int k=0;
    while(i<numA && j<numB){
        if(A[i]<B[j])
            C[k++] = A[i++];
        else C[k++]=B[j++];
    }

    while(i<numA)
        C[k++]=A[i++];

    while(j<numB)
        C[k++]=B[j++];

    for(int i=0;i<k;i++)
        arr[low+i]=C[i];

    free(A);
    free(B);
    free(C);

}
void MergeSort(int *arr,int low,int high){

    if(low<high){

        int mid=low+(high-low)/2;
        MergeSort(arr,low,mid);
        MergeSort(arr,mid+1,high);

        Merge(arr,low,mid,high);
    }
}
void BinaryInsertionSort(int *v,int low_arr,int high_arr){
    int ind;
    int aux,elem,low,high,mid;
    for(int i=low_arr;i<=high_arr;i++){
        low=0;
        high=i;
        elem=v[i];


        while(low<high){
            mid=low+(high-low)/2;

            if(v[mid]<=elem)
                low=mid+1;
            else high=mid;
        }

        if(high!=i) {
            for (int j = i; j > high; j--) {
                v[j] = v[j - 1];
            }
            v[high] = elem;
        }
    }
}
int Partition(int *arr,int low,int high){
    int aux;
    int pivot=low;
    while(low<=high){

        while(arr[low]<arr[pivot]) {
            low++;
        }

        while(arr[high]>arr[pivot]) {
            high--;
        }

        if(low<high){
            aux=arr[low];
            arr[low]=arr[high];
            arr[high]=aux;
            low++;
            high--;
        }
        else break;
    }
    aux=arr[high];
    arr[high]=arr[pivot];
    arr[pivot]=aux;

    return high;
}
void HybridQuickSortBinaryInsertionSort(int *arr,int low,int high,int threshold){
    if(low<high){
        if(high-low<threshold)
            BinaryInsertionSort(arr,low,high);
        else {

            int rand_index = rand() % (high - low) + low;
            int aux = arr[low];
            arr[low] = arr[rand_index];
            arr[rand_index] = aux;

            int p = Partition(arr, low, high);
            HybridQuickSortBinaryInsertionSort(arr, low, p - 1,threshold);
            HybridQuickSortBinaryInsertionSort(arr, p + 1, high,threshold);
        }
    }
}

int main() {

    freopen("algsort.in","r",stdin);
    freopen("algsort.out","w",stdout);
    int n;
    scanf("%d",&n);
    int *arr=(int *) malloc(sizeof(int)*n);
      for(int i=0;i<n;i++)
          scanf("%d",&arr[i]);

    //MergeSort(arr,0,n-1);
    HybridQuickSortBinaryInsertionSort(arr,0,n-1,23);

    for(int i=0;i<n;i++)
        printf("%d ",arr[i]);

    free(arr);
    return 0;
}