Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Sort the index of an array

I have an array that looks like this: a[]={2,3,4,5,8,2,5,6}.
Now I want to sort the indexes ,but keep the original array intact, and get something like this a_index[]={0,5,1,2,3,6,7,4}...

I have an O(N^2) algorithm for this. Can someone give me a better one (preferably O(NlogN))?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Create a struct that contains two fields: index and value.

Create an array of this structs, where each element (struct) is the original index and value of an element in the array.

Sort the struct using ONLY the value in O(nlogn).

When you are done - iterate the array in the sorted order to get the sorted indices.

over 4 years ago · Santiago Trujillo Relatório

0

use qsort E.g

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

int *TheArray;

int cmp(const void *a, const void *b){
    int ia = *(int *)a;
    int ib = *(int *)b;
    return (TheArray[ia] > TheArray[ib]) - (TheArray[ia] < TheArray[ib]);
}

int main(void) {
    int a[] = {2,3,4,5,8,2,5,6};
    size_t len = sizeof(a)/sizeof(*a);
    int a_index[len];
    int i;

    for(i = 0; i < len; ++i)
        a_index[i] = i;

    TheArray = a;
    qsort(a_index, len, sizeof(*a_index), cmp);

    for(i = 0; i < len; ++i)
        printf("%d ", a_index[i]);//5 0 1 2 6 3 7 4 : qsort is not a stable.
    printf("\n");

    return 0;
}
over 4 years ago · Santiago Trujillo Relatório

0

Wouldn't this essentially be solveable with an implementation of any sorting algorithm, with the following adjustment:

Where normally you would compare something like: if (a[x] < a[x+1])

You will now be doing: if (a[a_index[x]] < a[a_index[x+1]])

And instead of: swap(a[x], a[x+1])

You'll be doing: swap(a_index[x], a_index[x+1])

(Where a_index is initialized to contain the range of indexes in sequential order initially (0..sizeof(a))

Since essentially a_index is just a lookup-table, where the value for the purpose of sorting is the corresponding value in a. (In practice this is just another level of indirection compared to what we normally do when we sort, as normally we wouldn't compare (x) and (x+1) directly either)

A similar solution can be done without doing in-place sorting as well, as long as you perform all your comparisons against the corresponding values in a, instead of comparing the

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda