Insertion Sort

Preview:

Citation preview

Insertion SortInsertion Sort

CH Gowri KumarCH Gowri Kumar

gkumar007@gmail.comgkumar007@gmail.com

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

Insertion SortInsertion Sort

Consists of N-1 passesConsists of N-1 passes For pass P = 1 through N-1, insertion sort For pass P = 1 through N-1, insertion sort

ensures that the elements in positions 0 ensures that the elements in positions 0 through P are in the sorted orderthrough P are in the sorted order

In pass P, the element in the position P is In pass P, the element in the position P is moved left until its correct place is found moved left until its correct place is found among the first P + 1 elements.among the first P + 1 elements.

Complexity - O(nComplexity - O(n22))

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 8 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

34 64 51 32 21

8

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 32 21

64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 32 21

64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 32 21

64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 51 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 64 32 21

51

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 32 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 34 51 64 21

32

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64 21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 32 34 51 64

21

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 21 32 34 51 64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 21 32 34 51 64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 21 32 34 51 64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 21 32 34 51 64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

8 21 32 34 51 64

void InsertionSort(ElementType A[ ], int N){

int j, P;ElementType Tmp;for(P = 1;P<N; P++){

Tmp = A[P];for (j = P;j>0 && A[j-1] > Tmp;

j--)A[ j] = A[j-1];

A[j] = Tmp;}

}

April 12, 2023April 12, 2023 www.gowrikumar.comwww.gowrikumar.com

The EndThe End

Recommended