Output the indices of array in descending order - c

I use the following quicksort function to sort any given array in descending order:
int sorting (const void * a, const void * b)
{
return ( *(double*)a < *(double*)b );
}
int main(int argc, char *argv[]) {
int n;
double values[] = { 88.54, 56.65, 100.13, 2.091, 25.223 };
qsort(values, 5, sizeof(double), sorting);
for( n = 0 ; n < 5; n++ ) {
printf("%f ", values[n]);
}
return(0);
}
Besides outputting the values in descending order, I want to output their corresponding indices. For instance, for the given values[] array, I would get [2, 0, 1, 4, 3] which indicates the index 2 has the largest values, index 0 has the second largest values, and so on. How can I modify the code above ?
Thank you

Combine values with indexes in a struct, sort them, and print indexes along with values:
struct ind_val {
int index;
double value;
};
int sorting_ind_val (const void * a, const void * b) {
double lhs = ((struct ind_val*)a)->value;
double rhs = ((struct ind_val*)b)->value;
if (lhs < rhs)
return 1;
if (lhs > rhs)
return -1;
return 0;
}
...
double values[] = { 88.54, 56.65, 100.13, 2.091, 25.223 };
struct ind_val pair[5];
for (int i = 0 ; i != 5 ; i++) {
pair[i].index = i;
pair[i].value = values[i];
}
qsort(pair, 5, sizeof(struct ind_val), sorting_ind_val);
for (int i = 0 ; i != 5 ; i++) {
printf("%d: %f\n", pair[i].index, pair[i].value);
}
Demo.
2: 100.130000
0: 88.540000
1: 56.650000
4: 25.223000
3: 2.091000

Related

Im trying to find the max and min value and its respective index. However I cant get the indexmin

Find the minimum element of the array and its corresponding index.
I can't get the the minimum index to work. Do I add else statement under each if statement?
#include<stdio.h>
int main()
{
int array[10]={1,2,3,4,5,6,7,8,9,10} , i;
**//finding max and min, and its respective index**
int max = array[0] , min = array[0];
int indmin , indmax;
for( i = 0 ; i < 10 ; i++ )
{
if(array[i] > max)
{
max = array[i];
indmax = i;
}
if(array[i] < min)
{
min = array[i];
indmin = i;
}
}
//print the max and min value and its indexs
printf("\nMaximum element is %d\t index is %d", max , indmax);
printf("\nMinimum element is %d\t index is %d", min , indmin);
}
Initialize indmin and indmax. When defining the array leave out the size so it's derived from the data. When iterating over the array use sizeof(array) / sizeof(*array) to let compiler determine the size of the array instead of hard-coding it. Minimize scope of variable i. Use a function to print output for less duplication:
#include <stdio.h>
void print(const char *prompt, int value, int index) {
printf("%s element is %d\t index is %d\n", prompt, value, index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
int min = array[0];
int indmin = 0;
int max = array[0];
int indmax = 0;
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
if(array[i] > max) {
max = array[i];
indmax = i;
}
if(array[i] < min) {
min = array[i];
indmin = i;
}
}
print("Maximum", max, indmax);
print("Minimum", min, indmin);
}
You could refactor this by creating a struct to keep the value and index together:
#include <stdio.h>
struct value_index {
int value;
int index;
};
void print(const char *prompt, struct value_index *vi) {
printf("%s element is %d\t index is %d\n", prompt, vi->value, vi->index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
struct value_index min = { array[0], 0 };
struct value_index max = { array[0], 0 };
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
if(array[i] > max.value) {
max.value = array[i];
max.index = i;
}
if(array[i] < min.value) {
min.value = array[i];
min.index = i;
}
}
print("Maximum", &max);
print("Minimum", &min);
}
Or you could realize that you only need the original array along with the two indices. To make my version even better than #Fe2O3's answer, I used a macro to make mine smaller (and if bait works then I will claim mine is easier to read) :-)
#include <stdio.h>
void print(const char *prompt, int *arr, int index) {
printf("%s element is %d\t index is %d\n", prompt, arr[index], index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
int indmin = 0;
int indmax = 0;
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
#define CMP_AND_SET(OP, V) if(array[i] OP array[V]) V = i
CMP_AND_SET(<, indmin);
CMP_AND_SET(>, indmax);
#unset CMP_AND_SET
}
print("Maximum", array, indmax);
print("Minimum", array, indmin);
}
Building on #Fe2O3's branchless idea combined with an initialized array which I find to compact and quite readable:
indmin = (int[]) { indmin, i }[array[i] < array[indmin]];
indmax = (int[]) { indmax, i }[array[i] > array[indmax]];
By using (a < b) <=> -1 * (-a > -b) you can write the last one as (note: UB if array contains INT_MIN):
indmax = (int[]) { indmax, i }[-array[i] < -array[indmax]];
I would use a local macro to reduce code duplication by using macro to generate either the first version by passing in the operator (see above) or the 2nd version by passing in a factor F:
#define MINMAX(V, F) V = (int[]) { V, i }[F * array[i] < F * array[V]]
indmin = MINMAX(indmin, 1);
indmax = MINMAX(indmax, -1);
I am totally cheating but you can shuffle the min and max elements to fixed positions within the source array. No storage overhead. This would be the opposite of branchless.
#include <stdio.h>
void print(const char *prompt, int value) {
printf("%8s = %3d\n", prompt, value);
}
int swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
int main(void) {
int arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
const int min = 0;
const int max = sizeof arr/sizeof *arr - 1;
for(int i = 1; i < max + 1; i++ )
arr[i] < arr[min] && swap(arr + i, arr + min) ||
arr[i] > arr[max] && swap(arr + i, arr + max);
print("min", arr[min]);
print("max", arr[max]);
}
Leaving variables uninitialised is asking Demon of Hard-To-Find Bugs to co-author your code. Define variables close to where they are used to increase clarity. And, don't define more variables than you need. (Common beginner mistake to make another copy "just in case"...)
// use the spacebar to increase readability
#include <stdio.h>
int main() {
// let the compiler assign the size of an initialised array
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// use fewer variables
int indmin = 0, indmax = 0;
// don't compare an element (arr[0]) to itself
for( int i = 1 ; i < sizeof array/sizeof array[0]; i++ )
if( array[ i ] > array[ indmax ] )
indmax = i; // updated
else
if( array[ i ] < array[ indmin ] )
indmin = i; // updated
// don't add unnecessary braces (imho)
// this isn't the 17th century in need of needless filligree.
// use '\n' at the END of output. sometimes needed to 'flush' output buffer
printf("Maximum element is %d\t index is %d\n", array[ indmax ] , indmax);
printf("Minimum element is %d\t index is %d\n", array[ indmin ] , indmin);
return 0;
}
Maximum element is 10 index is 9
Minimum element is 1 index is 0
EDIT:
So, there's a friendly competition going on in this question... :-)
How's this:
#include <stdio.h>
int main() {
// let the compiler assign the size of an initialised array
// use shorter names to expose operations (not lengthy variable names)
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int iMin = 0, iMax = 0;
// don't compare an element to itself
for( int i = 1; i < sizeof arr/sizeof arr[0]; i++ ) {
// use "branchless" coding for speed.
int n = arr[i] > arr[iMax];
iMax = n*i + !n*iMax;
n = arr[i] < arr[iMin];
iMin = n*i + !n*iMin;
}
// reduce duplication of static data
char *fmt = "%s element is %d\t index is %d\n";
printf( fmt, "Maximum", arr[ iMax ], iMax );
printf( fmt, "Minimum", arr[ iMin ], iMin );
return 0;
}
Same output.
Ball's in your court #Allan :-)
EDIT:
There has been an advance on the last offering that needs to be addressed...
Here we go whole-hog, splurging-out with a third 'container' (mm[0]) to catch all those indexes that satisfy neither conditional ('<' & '>'). AND, a 4th 'container' (mm[3]) that doesn't change from being initialised to 0, the index of the 1st element. Besides being cryptic (not advised), this may-or-may-not be more expensive with its multiple array offset calculations... But, it's fun to look at...
#include <stdio.h>
int main() {
// added two elements to show 0 and nElem are not 'flukes'
// this really does find and report the min/max values
int arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
int i, mm[1 + 2 + 1] = { 0 };
// assign 'i' to mm[ 0 or 1 or 2 ]. 0=latest, 1=max, 2=min, (3 unaffected)
for( i = 1; i < sizeof arr/sizeof arr[0]; i++ )
mm[ (arr[i] > arr[mm[1]]) + 2*(arr[i] < arr[mm[2]]) ] = i;
mm[ 0 ] = i-1; // always pick up the last index. Thanks #A Wind!
// now... this is getting silly!!
char *fmt = "%5s = %3d # arr[%d]\n";
char *type[] = { "last", "max", "min", "first" };
i = 3; do printf( fmt, type[i], arr[ mm[i] ], mm[i] ); while( --i >= 0 );
return 0;
}
first = 1 # arr[0]
min = -42 # arr[6]
max = 42 # arr[4]
last = 10 # arr[11]
Y'know... This might be interesting to try to apply to 3-way branching as is needed for binary searching; determining '<', '=' or '>'... Hmmm...
EDIT: (another variation on a theme at the suggestion of a worthy competitor :-)
#include <stdio.h>
int main() {
struct {
char *label;
int ind;
} mm[] = {
{ "last" },
{ "maximum" },
{ "minimum" },
{ "first" },
};
int i, arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
for( i = 1; i < sizeof arr/sizeof arr[0]; i++ )
mm[ (arr[i] > arr[mm[1].ind]) + 2*(arr[i] < arr[mm[2].ind]) ].ind = i;
mm[ 0 ].ind = --i; // always pick up the last index. Thanks #A Wind!
for( i = sizeof mm/sizeof mm[0]; --i >= 0; /* space for rent */ )
printf( "%8s = %3d # arr[%d]\n", mm[i].label, arr[ mm[i].ind ], mm[i].ind );
return 0;
}
EDIT:
Trying to cover ALL the bases, here are three more ways to skin a cat
/* Minimalist */
#include <stdio.h>
int main() {
int mm[3] = { 0 },
arr[] = { 1, 2, 3, 4, 42, 5, 6, 7, 8, 9, 10 },
i = sizeof arr/sizeof arr[0];
while( --i )
mm[ 2*(arr[i] > arr[mm[2]]) + (arr[i] < arr[mm[1]]) ] = i;
char *fmt = "arr[%d] = %3d M%simum\n";
printf( fmt, mm[1], arr[mm[1]], "in" );
printf( fmt, mm[2], arr[mm[2]], "ax" );
return 0;
}
/* Recursive - for brevity, excluding the index; just reporting two values */
#include <stdio.h>
int amin( int a[], int i ) { // NB: "amin", not "main"
int j = --i ? amin( a, i ) : i;
return (a[j]<a[i])*j + (a[j] > a[i])*i;
}
int amax( int a[], int i ) {
int j = --i ? amax( a, i ) : i;
return (a[j]>a[i])*j + (a[j]<a[i])*i;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, },
sz = sizeof arr/sizeof arr[0];
char *fmt = "M%simum: %3d\n";
printf( fmt, "in", arr[ amin(arr, sz) ] );
printf( fmt, "ax", arr[ amax(arr, sz) ] );
return 0;
}
/* And, simply brute force using a library function */
#include <stdio.h>
#include <stdlib.h>
int cmp( const void *a, const void *b ) { return *(int*)a - *(int*)b; }
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
sz = sizeof arr/sizeof arr[0];
qsort( arr, sz, sizeof arr[0], cmp );
char *fmt = "M%simum: %3d\n";
printf( fmt, "in", arr[ 0 ] );
printf( fmt, "ax", arr[ --sz ] ); // again, thanks to #A Wind
return 0;
}
Many ways to skin a cat.

recursive find number in between in C

I want to find the number within a range in an array and must be in a recursive way. The function variables couldn't be modified.
Let's say in the range of 2 and 3
The input is : int a[] = {4, 1, 3, 1, 3, 2};
and the output will be = {3,3,2} , 3 found
Not sure how to code the recursive function in this case. The below I have tried not working.
int within(int a[], int N, int lower, int upper, int result[])
{
if(N == 1 && N <= upper && N>= lower)
return a[0];
return within(&a[1], N-1, lower, upper, result);
}
int main()
{
int a[] = {4, 1, 3, 1, 3, 2};
int result[6] = {0};
int i, nResult;
nResult = within(a, 6, 2, 3, result);
printf("%d data passed the bounds\n", nResult);
for (i = 0; i < nResult; i++){
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
I want to find the number within a range in an array
Let's say in the range of 2 and 3
Normally a for loop or similar would be so much easier here
If it has to be recursive....
// need to have another number - r - number in range
// r starts at zero
//
// normally lower case for variable and capitals for things you #define
// N starts at the number of elements of a less one
//
int within(int a[], int N, int lower, int upper, int r, int result[])
{
if(a[0] <= upper && a[0]>= lower) {
result[r]= a[0];
r++;
}
if(N==0) {
return r;
} else {
r = within(&a[1], N-1, lower, upper, r, result);
return r;
}
}
the function will give a return value of the number of values found within the range.
The code above is recursive, but so much more complicated and fragile than a simple loop... such as the fragment below
for (i=0;i<N;i++) {
if(a[i] <= upper && a[i]>= lower) {
result[r]= a[i];
r++;
}
}
If it has to be recursive wihtout r...
// need to have another number - result[0] - number in range
// result[0] starts at zero
//
// normally lower case for variable and capitals for things you #define
// N starts at the number of elements of a less one
//
int within(int a[], int N, int lower, int upper, int result[])
{
if(a[0] <= upper && a[0]>= lower) {
result[0]++;
result[result[0]]= a[0];
}
if(N==0) {
return result[0];
} else {
result[0] = within(&a[1], N-1, lower, upper, result);
return result[0];
}
}
now result conatins
{number in range, first number in range, second number in range....}
Something like this. If you want to implement a recursive function, try to do it in the way that the recursive call happens at the end.
#include <stdio.h>
int find_in_range(int* out, int const *in, int length, int from, int to)
{
if (length == 0)
{
return 0;
}
int addon;
if (*in >= from && *in <= to)
{
*out = *in;
++out;
addon = 1;
}
else
{
addon = 0;
}
return find_in_range(out, in + 1, length - 1, from, to) + addon;
}
#define N 6
int main()
{
int in[N] = {4, 1, 3, 1, 3, 2};
int out[N] = {0};
int num_found = find_in_range(out, in, N, 2, 3);
for (int i = 0; i < num_found; ++i)
{
printf("%d ", out[i]);
}
printf("\n");
return 0;
}
You can modify the following code as per your requirements. This is just a proof of concept code:
#include <stdio.h>
#include <stdlib.h>
static int result[4];
static int ctr1 = 0;
static int ctr2 = 0;
void recFind(int* arr, int* key){
if(ctr2 == 8)
return;
if(*arr >= key[0] && *arr <= key[1])
result[ctr1++] = *arr;
arr++;
ctr2++;
recFind(arr, key);
}
int main(){
int arr[] = {1,3,3,6,4,6,7,8};
int key[] = {1,4};
recFind(arr, key);
printf(" { ");
for(int i = 0; i < 4; i++){
printf("%d ", result[i]);
}
printf("}\n");
}
As it follows from the description of the assignment the function should provide two values: the number of elements that satisfy the condition and an array that contains the elements themselves.
It is evident that the array should be allocated dynamically. And it is logically consistent when the function itself returns the number of elements while the pointer to the generated array is passed by reference as an argument.
The recursive function can look the following way
#include <stdio.h>
#include <stdlib.h>
size_t get_range( const int a[], size_t n, int lower, int upper, int **out )
{
size_t m;
if ( n )
{
m = get_range( a, n - 1, lower, upper, out );
if ( lower <= a[n-1] && a[n-1] <= upper )
{
int *tmp = realloc( *out, ( m + 1 ) * sizeof( int ) );
if ( tmp )
{
tmp[m] = a[n-1];
*out = tmp;
++m;
}
}
}
else
{
*out = NULL;
m = 0;
}
return m;
}
int main(void)
{
int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
int lower = 2, high = 3;
int *out;
size_t n = get_range( a, N, lower, high, &out );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", out[i] );
}
putchar( '\n' );
free( out );
return 0;
}
The program output is
2 3 3 2
Below codes will work for you in recursive way. If you don't want to print the numbers just comment out printf statement inside function printfRange. Hope you can understand the logic :-
int within(int *a, int rngH, int rngL, int length)
{
int len = length;
static int i = 0;
static int found = 0;
if(len <=0 )
{
return i;
}
if (*a == rngH)
{
printf("%d,",*a);
i++;
found = 1;
within(++a,rngH, rngL,--len);
}
else if(*a == rngL && found > 0)
{
printf("%d,",*a);
i++;
within(++a,rngH, rngL,--len);
}
else
{
within(++a,rngH, rngL,--len);
}
return i;
}
int main() {
int a[] = {4, 1, 3, 1, 3, 2};
int total = within(a,3,2,6);
printf("\n");
printf("Total :%d\n",total);
return 0;
}

how to compare two arrays in c?

I am creating a function which can compare two arrays. It returns 1 when they are the same and return 0 when they are not.
It required the program run as linear time, so i cannot use a for-for loop to compare it. Any suggestions for me?
Examples of arrays for which scrambled should return 1:
a = {10,15,20}, b = {10,15,20}
a = {1,2,3,4,5}, b = {5,3,4,2,1}
a = {}, b = {} (i.e. len = 0)
a = {2,1,3,4,5}, b = {1,2,4,3,5}
Examples of arrays for which scrambled should return 0:
a = {1,1}, b = {1,2}
a = {10,15,20}, b = {10,15,21}
a = {1,2,3,4,5}, b = {5,3,4,2,2}
If you can specify a maximum value for the array elements, you can compare them in linear time pretty easily, by just looping through each one and counting the values which are present, like so:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_ARRAY_VALUE 100
bool compare_arrays(int * arr1, size_t arr1_size,
int * arr2, size_t arr2_size)
{
/* Set up array to count elements */
int * table = calloc(MAX_ARRAY_VALUE + 1, sizeof * table);
if ( !table ) {
perror("couldn't allocate memory");
exit(EXIT_FAILURE);
}
/* Increment index if element is present in first array... */
for ( size_t i = 0; i < arr1_size; ++i ) {
table[arr1[i]]++;
}
/* ...and decrement index if element is present in second array. */
for ( size_t i = 0; i < arr2_size; ++i ) {
table[arr2[i]]--;
}
/* If any elements in array are not zero, then arrays are not equal */
for ( size_t i = 0; i < MAX_ARRAY_VALUE + 1; ++i ) {
if ( table[i] ) {
free(table);
return false;
}
}
free(table);
return true;
}
int main(void) {
int a1[] = {10, 20, 30, 10};
int a2[] = {20, 10, 10, 30};
int a3[] = {1, 4, 5};
int a4[] = {1, 3, 5};
if ( compare_arrays(a1, 4, a2, 4) ) {
puts("a1 and a2 are equal"); /* Should print */
}
else {
puts("a1 and a2 are not equal"); /* Should not print */
}
if ( compare_arrays(a3, 3, a4, 3) ) {
puts("a3 and a4 are equal"); /* Should not print */
}
else {
puts("a3 and a4 are not equal"); /* Should print */
}
if ( compare_arrays(a1, 4, a4, 3) ) {
puts("a1 and a4 are equal"); /* Should not print */
}
else {
puts("a1 and a4 are not equal"); /* Should print */
}
return 0;
}
which outputs:
paul#horus:~/src/sandbox$ ./lincmp
a1 and a2 are equal
a3 and a4 are not equal
a1 and a4 are not equal
paul#horus:~/src/sandbox$
Without specifying the maximum value, you can loop through each array and find the maximum. It'll still be linear time, but without an upper bound you might end up with a huge index table.
Because the comparison of the two arrays is independent of the order of the elements, both must be sorted before they can be compared. Because of this, you can't do this in linear time. The best you can do is O(n log n), as that is the best order of most sorting algorithms.
My C is pretty rusty so the may be some memory problem with the script below. However, the basic task is to sort the 2 arrays, compare them element by element. If they all match, scramble should return 1, other it's 0.
The script below works with GCC, haven't tested it with any other compiler. It compares arrays of equal length. The case of unequal length is left for the OP as an minor exercise.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare_ints(const void * a , const void * b)
{
const int * ia = (const int *)a;
const int * ib = (const int *)b;
return *ia > *ib;
}
int scramble(const int * a, const int * b, unsigned int len)
{
int * sorted_a = malloc(len * sizeof(int));
int * sorted_b = malloc(len * sizeof(int));
memcpy(sorted_a, a, len * sizeof(int));
memcpy(sorted_b, b, len * sizeof(int));
qsort(sorted_a, len, sizeof(int), compare_ints);
qsort(sorted_b, len, sizeof(int), compare_ints);
for (int i = 0; i < len; i++)
{
if (sorted_a[i] != sorted_b[i])
{
free(sorted_a);
free(sorted_b);
return 0;
}
}
free(sorted_a);
free(sorted_b);
return 1;
}
int main (int argc, char const *argv[])
{
int a[3] = {20, 10, 15};
int b[3] = {10, 15, 20};
int is_equal = scramble(a, b, 3);
printf("is_equal = %i\n", is_equal);
return 0;
}
FYI: you can't do it in linear time. qsort has O(n log n).
At a certain extent, you can obtain the sum of all the hashed values of the first array, and compare it to the sum of the hashed values of the second array. It'll work, but I don't know precise it is.
Here is my attempt, all my tests have given positive results so far :
#include <stdio.h>
unsigned int hash(unsigned int x) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x);
return x;
}
int compareArrays(int *arr1, int *arr2, int size) {
if (size == 0) return 1;
unsigned long sum1 = 0;
unsigned long sum2 = 0;
for (int i = 0; i < size; ++i) {
sum1 += hash(arr1[i]);
}
for (int i = 0; i < size; ++i)
sum2 += hash(arr2[i]) ;
return sum1 == sum2;
}
int main(void) {
int a[] = {1,2,3,4,5,6,7,8,255};
int b[] = {1,2,3,4,5,6,7,8,9};
int size = 9;
printf("Are they the same? %s.\n", compareArrays(a, b, size) ? "yes" : "no");
return 0;
}
You can use memcmp function with this format:
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
For example;
/* memcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char buffer1[] = "abcde";
char buffer2[] = "abcde";
int n;
n=memcmp ( buffer1, buffer2, sizeof(buffer1) );
if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);
return 0;
}
output is:
'abcde' is same as 'abcde'.

how to find complete sorting of elements by frequency?

Here is the problem:
Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}. if 2 numbers have same frequency then print the one which came 1st.
I know how to do it partially. Here is my approcach.
I will create a struct which will be like:
typedef struct node
{
int index; // for storing the position of the number in the array.
int count; // for storing the number of times the number appears
int value; // for storing the actual value
} a[50];
I will create an array of these structs, I will then sort it by a sorting algorithm on the basis of their count. However, how can I ensure that if the frequency of two elements are same, then that number should appear which has a lesser index value?
#include <stdlib.h> // qsort, malloc, free
#include <stddef.h> // size_t
#include <stdio.h> // printf
struct number
{
const int * value;
int num_occurrences;
};
static void cmp_by_val(const struct number * a, const struct number * b)
{
if (*a->value < *b->value)
return -1;
else if (*b->value < *a->value)
return 1;
else
return 0;
}
static void cmp_by_occurrence_stable(const struct number * a, const struct number * b)
{
if (a->num_occurrences < b->num_occurrences)
return -1;
else if (b->num_occurrences < a->num_occurrences)
return 1;
else if (a->value < b->value)
return -1;
else if (b->value < a->value)
return 1;
else
return 0;
}
static struct number * sort_by_occurrence(const int * arr, size_t N)
{
//
// STEP 1: Convert the input
//
struct number * sort_arr = (struct number *)malloc(N * sizeof(struct number));
if (! sort_arr) return NULL;
for (int k = 0; k < N; ++k)
{
sort_arr[k].value = &arr[k];
sort_arr[k].num_occurrences = 0;
}
//
// STEP 2: Sort the input based on value
//
qsort(sort_arr, N, sizeof(struct number), cmp_by_val);
//
// STEP 3: Count occurrences
//
if (0 < N)
{
int cur_value = *sort_arr[0].value;
int i = 0;
for (j = 1; j < N; ++j)
{
if (*sort_arr[j].value != *sort_arr[i].value)
{
for (int k = i; k < j; ++k)
sort_arr[k].num_occurrences = j - i;
i = j;
}
}
for (; i < N; ++i)
sort_arr[i].num_occurrences = N - i;
}
//
// STEP 4: Sort based on occurrence count
//
qsort(sort_arr, N, sizeof(struct number), cmp_by_occurrence_stable);
//
// DONE
//
return sort_arr;
}
static void print_arr(const struct number * arr, size_t N)
{
if (0 < N)
{
printf("%d", arr[0]->value);
for (int k = 1; k < N; ++k)
printf(", %d", arr[k]->value);
}
printf("\n");
}
int main(int argc, char ** argv)
{
const int EXAMPLE_INPUT[11] = { 2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12 };
struct number * sort_arr = sort_by_occurrence(EXAMPLE_INPUT, 11);
if (sort_arr)
{
print_arr(sort_arr, 11);
free(sort_arr);
}
};
You could create an array which stores the frequency of your input array (i.e. frequency[i] is the frequency of the input[i] element). After that it is easy to order the frequency array (using an stable algorithm) and make the same changes (swaps?) to the input array.
For creating the frequency array you can use several approaches, a simple and inefficient one is just counting each element with two nested loops. I left more efficient alternatives to your imagination.
Note: the frequency array has the same function as the count field in your struct node, but in a separated memory. If you will not need the frequencies in the future, I recommend you to use the separated memory, as you can release it.
It seems that the problem is using unstable sort algorithm on the frequency of array elements.
Do a qsort on the array based on freq
Again do a qsort on the resulted array based on the indexes of the element with the same freq only.
This should give you a correct answer in O(nLog)
I minimized the code. The obvious parts are left out.
struct node
{
int *val;
int freq;
// int index; <- we can do this by comparing &a->val with &b->val
};
int compare_byfreq(const int* a, const int* b)
{
return a->freq - b->freq;
}
int compare_index(const int* a, const int* b)
{
if( a->freq == b->freq)
{
return a->val - b->val; //this can never be zero
}
//else we have different freq don't move elem
return 0;
}
int main()
{
int arr[] = {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12};
node *narray = (struct node*)malloc(sizeof(arr) * sizeof(node));
// build the nodes-array
for(int i =0; i < sizeof(arr); i++)
{
/* buid narray here, make sure you store the pointer to val and not the actual values */
}
qsort(narray, sizeof(arr), compare_byfreq);
qsort(narray, sizeof(arr), compare_index);
/*print narray*/
return 0;
}
Edit: #0xbe5077ed got an interesting idea. Instead of comparing indexes compare addresses of your values! - I just re-edited the code for that
I was trying to learn Java nowadays, realized that this could be a good exercise. Tried and solved this problem over there in Eclipse. Java is horrible, I went back to C to solve it, here's a solution that I'll explain right after showing it:
#include <stdio.h>
#include <malloc.h>
typedef struct numbergroup {
int firstencounteridx;
int count;
int thenumber;
} Numbergroup;
int firstoneissuperior( Numbergroup gr1, Numbergroup gr2 ) {
return gr1.count > gr2.count || // don't mind the line-break, it's just to fit
( gr1.count == gr2.count && gr1.firstencounteridx < gr2.firstencounteridx );
}
void sortgroups( Numbergroup groups[], int amount ) {
for ( int i = 1; i < amount; i++ ) {
for ( int j = 0; j < amount - i; j++ ) {
if ( firstoneissuperior( groups[j + 1], groups[j] ) ) {
Numbergroup temp = groups[j + 1];
groups[j + 1] = groups[j];
groups[j] = temp;
}
}
}
}
int main( ) {
int input[] = { 2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12 };
Numbergroup * groups = NULL;
int amountofgroups = 0;
for ( int i = 0; i < ( sizeof input / sizeof * input ); i++ ) {
int uniqueencounter = 1;
for ( int j = 0; j < amountofgroups; j++ ) {
if ( groups[j].thenumber == input[i] ) {
uniqueencounter = 0;
groups[j].count++;
break;
}
}
if ( uniqueencounter ) {
groups = realloc( groups, ( amountofgroups + 1 ) * sizeof * groups );
groups[amountofgroups].firstencounteridx = i;
groups[amountofgroups].count = 1;
groups[amountofgroups].thenumber = input[i];
amountofgroups++;
}
}
sortgroups( groups, amountofgroups );
for ( int i = 0; i < amountofgroups; i++ )
for ( int j = 0; j < groups[i].count; j++ )
printf( "%d ", groups[i].thenumber );
free( groups );
putchar( 10 );
return 0;
}
Let me explain the structure first, as well as its functionality: It is for each unique number. In your example, it is for 2s, 3s, 4s, 5s and the 12s, one for each, 5 in total. Each one is to store:
the index of the first encounter of that number
the amount of encounter of that number
the value of that number
For example, for 12s, it shall store:
firstencounteridx as 5, that is the index of the first 12
count as 2
thenumber as 12
The first loop generally does that. It expands the group of Numbergroups whenever a unique number is encountered, stores its index as well; increases the count in case a number that already has a group has been encountered.
Then a sort is issued, which simply is a bubble sort. Might be different than the conventional one, I don't have any memorized.
Sorting criteria function simply checks if the count field of the first group is greater than the other; otherwise it checks whether they are the same and the firstencounter of the first group is earlier than the other; in which cases it returns 1 as true. Those are the only possible ways for the first group to be considered superior than the second one.
That's one method, there can be others. This is just a suggestion, I hope it helps you, not just for this case, but in general.
Created a map and sort the map by value.
O(nlogn) time, and O(n) space.
import java.util.*;
public class SortByFrequency {
static void sortByFreq( int[] A ) {
// 1. create map<number, its count>
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < A.length; i++) {
int key = A[i];
if( map.containsKey(key) ) {
Integer count = map.get(key);
count++;
map.put(key, count);
}
else {
map.put(key, 1);
}
}
// 2. sort map by value in desc. order
// used modified (for desc. order) MapUtil in http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java
Map<Integer, Integer> map2= MapUtil.sortByValue(map);
for(Map.Entry<Integer, Integer> entry : map2.entrySet() ) {
int num = entry.getKey();
int count = entry.getValue();
for(int i = 0; i < count; i++ ) {
System.out.print( num + " ");
}
}
System.out.println();
}
public static void main(String[] args ) {
int[] A1 = {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12};
sortByFreq(A1);
}
}

Sort an array by an index array in C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In-place array reordering?
I have original unsorted array with structures such as:
{D, A, B, E, C}
and array of indexes of original array in sorted order:
{2, 3, 5, 1, 4} // Edited. Then I get {A, B, C, D, E}.
How can I simply rearranged the originial array by an index array?
I can't create new array and insert elements by index-position.
My 5 cents:
int new_i;
for (int i = 0; i < arr_size-1; ++i)
{
while ((new_i = index_arr[i]-1) != i)
{
std::swap(struct_arr[i], struct_arr[new_i]);
std::swap(index_arr[i], index_arr[new_i]);
}
}
O(N^2) solution:
struct S[] = {D, A, B, E, C};
int o[] = {2, 3, 5, 1, 4};
int len = 5;
for (int i=1;i<=len;++i) {
for(int j=i-1; j<len;++j) {
if(o[j]==i) {
if (i!=j+1) {
swapSArrayItems(S, j, i-1);
swapIntArrayItems(o, j, i-1);
}
break;
}
}
}
Here's a version that works, but note that it does invalidate the indices variable:
void sortItemsBasedOnIndices(char *items[], int indices[], size_t num)
{
// sort them
for (int i = 0; i < num; i++)
{
int newIndex = indices[i];
// take out the item at the specified index
char *newItem = items[newIndex];
// take out the item in that current position
char *oldItem = items[i];
// swap the two items
items[i] = newItem;
items[newIndex] = oldItem;
// now, tell the sorted indices the location of the old item after swap
for (int j = i; j < num; j++)
{
if (indices[j] == i)
{
indices[j] = newIndex;
break; // we only need the first one, so then we're done
}
}
}
}
int main()
{
char *items[] = { "D", "B", "E", "A", "C" };
int sortedIndices[] = { 3, 1, 4, 0, 2 }; // 0-based
int size = 5;
sortItemsBasedOnIndices(items, sortedIndices, size);
for (int i = 0; i < size; i++)
{
puts(items[i]);
}
}
#include <stdio.h>
void shuffle( char **arr, int *offs, size_t cnt);
void shuffle( char **arr, int *offs, size_t cnt)
{
unsigned idx,src,dst;
for (idx=0; idx < cnt; idx++) offs[idx] -= idx;
for (idx=0; idx < cnt; idx++) {
if (offs[idx] == 0) continue;
char *tmp;
tmp = arr[idx];
for(dst = idx; offs[dst] ; dst=src) {
src = dst+offs[dst] ;
arr[dst] = arr[src];
offs[dst] = 0;
if (offs[src] == 0 ) break;
}
arr[dst]=tmp;
}
}
int main (void)
{
unsigned idx;
char *array[5] = {"D","A","B","E","C"};
int index[5] = {1,2,4,0,3};
fprintf(stdout, "Original:\n");
for (idx=0; idx < 5; idx++) {
fprintf(stdout, "[%u]:%s\n", idx, array[idx] );
}
fprintf(stdout, "Shuffle:\n");
shuffle( array, index, 5);
fprintf(stdout, "After shuffle:\n");
for (idx=0; idx < 5; idx++) {
fprintf(stdout, "[%u]:%s\n", idx, array[idx] );
}
return 0;
}
Update: fixed end of chain condition (ugly!)

Resources