Get sorted index orderting of an array

Yesterday i was translating some code i wrote in R to C++. I had some calculation to do which required the list of index of the top n values in a list. Therefore what i needed was not a the list sorted itself or get a sorted copy of a given list, but actually the sorted order of the index into the actual array. To describe the problem, for example consider the following list:

arr = 20 50 40 80 10

The sorted (non-decreasing) ordering of the values is obviously

sorted_arr = 10 20 40 50 80

The sorted (non-decreasing) ordering of the index into the original array is (index starts at 1 in this example)

sorted_index_into_arr = 5 1 3 2 4

Therefore the smallest value in the list arr could be found by indexing into arr using the first value of sorted_index_into_arr, which stores the index of the array arr holding the smallest value.

arr[ sorted_index_into_arr[1] ]

The sorted index ordering is very easy to get in languages like R and Octave or Matlab. For example in R we can do the following to get the sorted index order using the order function:

> arr <- c (20, 50, 40, 80, 10)
> arr
[1] 20 50 40 80 10
> order (arr)
[1] 5 1 3 2 4

In the case of Octave or Matlab you can get the index order using the sort function, which will return two lists (1xn matrix), the first list is the sorted array itself, and the next one is the sorted index order into the original array.

octave:1> a = [20 50 40 80 10]
a =

   20   50   40   80   10

octave:2> [sorted index] = sort (a)
sorted =

   10   20   40   50   80

index =

   5   1   3   2   4

Although these functional languages provides gives these features in C/C++ it is not immediately available using the builtin sort library functions. But the sorting routines accepts a function pointer to a comparison function, writing appropriate code for which will do the trick.
Continue reading “Get sorted index orderting of an array”

Builtin Bash any base to decimal conversion

Bash has an interesting builtin feature to convert from any base to decimal, which is a part of bash’s arithmetic evaluation features. In this post i will quickly introduce you with this feature.
Continue reading “Builtin Bash any base to decimal conversion”

C Q&A #2: Watch your shift!

What do you this should be the output of the below code? Is there any problem in the code?

#include <stdio.h>

int main(void)
{
  int x = -1, y = 1, a, b, c, d, e, f;

  a = x << 4;
  b = x >> 4;
  c = y << 4;
  d = y >> 4;
  e = y << sizeof (int) * 8;
  f = y << -4;

  printf("a = %x \nb = %x \nc = %x \nd = %x \ne = %x \nf = %x",a, b, c, d, e, f);

  printf ("\n");
  return 0;
}

Continue reading “C Q&A #2: Watch your shift!”

Language Problem: Hmm…

So, people are into python. Hmm it seems that it is pretty useful stuff. Even Sebastian Thrun codes in python (knew it from Udacity) :D. I think python with the statistical, numerical and graph plotting packages or using Octave (no money for Matlab and its not FOSS) will be very helpful for me to quickly implement prototypes for different applications of learning algorithms and test them. Continue reading “Language Problem: Hmm…”

Find Sum (i … j) in a list

The problem is to sum the ith to jth element of a given list. The easiest solution runs in \mathcal{O}(n) time, which starts are the ith index and adds up the numbers in the list until it reaches the jth index. In this post i will show the process of calculating the sum of elements i through j both inclusive (where i \le j), which takes \mathcal{O}(1) time for finding the sum, and \mathcal{O}(n) time and space for preprocessing the given list of length n.
Continue reading “Find Sum (i … j) in a list”

Find a String in an Alphabet Grid

Everybody of us has definitely played the word search game in which a grid of characters are given, and a list of words are given which are to be found in the grid horizontally, vertically or diagonally. In this post i will show a simple solution to a more generalized version of the search which finds the strings in the grid with backtracking.
Continue reading “Find a String in an Alphabet Grid”

Towers of Hanoi Iterative process: simulating recursion

The last post Recursive Solution to Towers of Hanoi described the well-known recursive definition and implementation of the Towers of Hanoi problem. This post is an extension presenting the same problem iteratively by simulating the recursion stack. This implementation will simply to simulate the recursion presented on the previous post by using an explicit manual stack.

Continue reading “Towers of Hanoi Iterative process: simulating recursion”