Experiment: 1 Insertion Sort Algorithm: Theory
Experiment: 1 Insertion Sort Algorithm: Theory
Experiment: 1 Insertion Sort Algorithm: Theory
PSEUDOCODE:
Uses: Insertion sort is used when number of elements is small. It can also be useful
when input array is almost sorted, only few elements are misplaced in complete big
array.
EXPERIMENT: 2
Pseudocode:
int pivot=a[start],p1=start+1,i,temp;
for(i=start+1;i<=end;i++)
{
if(a[i]<pivot)
{
if(i!=p1)
{
temp=a[p1];
a[p1]=a[i];
a[i]=temp;
} p1++;
}
}
a[start]=a[p1-1];
a[p1-1]=pivot;
return p1-1;
}
class QuickSortPart1{
public int partition(int[] a, int left, int right) {
int pivot = a[left];
while(left<=right) {
while(a[left] < pivot)
left++;
while(a[right] > pivot)
right--;
if(left<=right) {
int tmp = a[left];
a[left] = a[right];
a[right] = tmp;
left++;
right--;
}
}
return left;
}
public void recursiveQuickSort(int[] a, int i, int j) {
int idx = partition(a, i, j);
if(i < idx-1) {
recursiveQuickSort(a, i, idx-1);
}
if(j > idx) {
recursiveQuickSort(a, idx, j);
}
}