Dynamic 2D array:
#include
#include
#include
int main()
{
int r,c;
clrscr();
printf(“Enter the size of the 2d array in (RXC) format :”);
scanf(“%dx%d”,&r,&c);
int *data=(int *)malloc(r*c*sizeof(int));
int index=1;
// Taking Input from the user in array
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
index=r;
index*=i;
index+=j;
printf(“Enter number : %d -> “,index);
scanf(“%d”,(data+index));
}
}
//Print the values from the array.
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
index=r;
index*=i;
index+=j;
printf(“\nValue No. %d stored in Array(%d,%d) is %d”,index,i,j,*(data+index));
}
}
getch();
}
QuickSort
public class QuickSort {
public static int size = 10;
public int[] table = new int[size];
public void init() {
for (int i = 0; i < size; i = i + 1)
table[i] = ((int) (java.lang.Math.random() * 100));
qSort(table, 0, size – 1);
}
public void qSort(int[]l, int left, int right) {
int i, last;
if (!(left >= right)) {
swap(l, left, (left + right) / 2);
last = left;
for (i = left + 1; i <= right; i = i + 1)
if (l[i] < l[left])
swap(l, last = last + 1, i);
swap(l, left, last);
qSort(l, left, last – 1);
qSort(l, last + 1, right);
}
}
public void swap(int[]l, int first, int second) {
int tmp;
tmp = l[first];
l[first] = l[second];
l[second] = tmp;
}
}