Suffix array construction in C++ with qsort and strcmp

Updated

A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suffix Tree which is compressed trie of all suffixes of the given text.

Let the given string be "banana".

0 banana                          5 a
1 anana     Sort the Suffixes     3 ana
2 nana      ---------------->     1 anana  
3 ana        alphabetically       0 banana  
4 na                              4 na   
5 a                               2 nana


Index       Suffix          Rank        Next Rank
  5          a               0             -1
  3          ana             1              0 
  1          anana           1              1      
  0          banana          2              3
  4          na              3             -1
  2          nana            3              3      

The suffix array is  {5,3,1,0,4,2}

Note : – Here, Pointer Approach is used to find suffix array with complexity O(n^2 logn) which is less complex and dense than other methods and require only the knowledge of pointers.

Algorithm –

(1) Initialize an array SuffixArr with index from 0 till length of string-1.

(2) Use this array to sort the Suffixes of the string in ascending order. I have used quick sort technique

(3) In the quick sort function , st + *((int*)a)) refers to the substring starting from that index and then sorting it according to its order , thus placing its index at respective position.

(4) Here , a and (int*)a provides address and *((int*)a) provides the index from which the suffix is taken. For e.g. – banana at a=1 generates string anana.

(5) After the sort function , the SuffixArr contains the Suffix Array.

#include <bits/stdc++.h>
using namespace std;
#define MAX 100

//Stores the string
char st[7];
//Stores the array
int SuffixArr[MAX];

//Used with the quick sort function
int cmp(const void *n1,const void *n2) 
{
	// Used to compare the suffix of strings
	return (strcmp((st+ *((int*)n1)),(st+ *((int*)n2))));
}

//Used to calculate Suffix Array 
void SuffixArray(int no) 
{
	
	for(int i=0;i<no;i++) 
	SuffixArr[i]=i;
	//Calls the quick sort function to calculate suffix array
	qsort(SuffixArr,no,sizeof(int),cmp);
	//Print the array
	for(int i=0;i<no;i++) 
	cout<<SuffixArr[i]<<" ";
}
//Driver Function
int main() 
{
	//Initializing the st array
	    st[0]='B';
	    st[1]='A';
	    st[2]='N';
	    st[3]='A';
	    st[4]='N';
	    st[5]='A';
	    
	    //Calculates the length
		int length = strlen(st);
		//Calls the function for printing the Suffix Array
		SuffixArray(length);
		return 0;
}
	
Comment
Mithlesh Upadhyay Published 5 Dec 2026 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials