89
Insertion Sort Insertion Sort CH Gowri Kumar CH Gowri Kumar [email protected] [email protected]

Insertion Sort

Embed Size (px)

Citation preview

Page 1: Insertion Sort

Insertion SortInsertion Sort

CH Gowri KumarCH Gowri Kumar

[email protected]@gmail.com

Page 2: Insertion Sort

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))

Page 3: Insertion Sort

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;}

}

Page 4: Insertion Sort

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;}

}

Page 5: Insertion Sort

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;}

}

Page 6: Insertion Sort

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;}

}

Page 7: Insertion Sort

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;}

}

Page 8: Insertion Sort

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;}

}

Page 9: Insertion Sort

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;}

}

Page 10: Insertion Sort

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;}

}

Page 11: Insertion Sort

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;}

}

Page 12: Insertion Sort

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;}

}

Page 13: Insertion Sort

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;}

}

Page 14: Insertion Sort

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;}

}

Page 15: Insertion Sort

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;}

}

Page 16: Insertion Sort

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;}

}

Page 17: Insertion Sort

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;}

}

Page 18: Insertion Sort

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;}

}

Page 19: Insertion Sort

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;}

}

Page 20: Insertion Sort

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;}

}

Page 21: Insertion Sort

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;}

}

Page 22: Insertion Sort

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;}

}

Page 23: Insertion Sort

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;}

}

Page 24: Insertion Sort

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;}

}

Page 25: Insertion Sort

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;}

}

Page 26: Insertion Sort

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;}

}

Page 27: Insertion Sort

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;}

}

Page 28: Insertion Sort

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;}

}

Page 29: Insertion Sort

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;}

}

Page 30: Insertion Sort

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;}

}

Page 31: Insertion Sort

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;}

}

Page 32: Insertion Sort

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;}

}

Page 33: Insertion Sort

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;}

}

Page 34: Insertion Sort

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;}

}

Page 35: Insertion Sort

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;}

}

Page 36: Insertion Sort

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;}

}

Page 37: Insertion Sort

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;}

}

Page 38: Insertion Sort

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;}

}

Page 39: Insertion Sort

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;}

}

Page 40: Insertion Sort

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;}

}

Page 41: Insertion Sort

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;}

}

Page 42: Insertion Sort

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;}

}

Page 43: Insertion Sort

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;}

}

Page 44: Insertion Sort

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;}

}

Page 45: Insertion Sort

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;}

}

Page 46: Insertion Sort

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;}

}

Page 47: Insertion Sort

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;}

}

Page 48: Insertion Sort

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;}

}

Page 49: Insertion Sort

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;}

}

Page 50: Insertion Sort

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;}

}

Page 51: Insertion Sort

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;}

}

Page 52: Insertion Sort

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;}

}

Page 53: Insertion Sort

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;}

}

Page 54: Insertion Sort

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;}

}

Page 55: Insertion Sort

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;}

}

Page 56: Insertion Sort

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;}

}

Page 57: Insertion Sort

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;}

}

Page 58: Insertion Sort

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;}

}

Page 59: Insertion Sort

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;}

}

Page 60: Insertion Sort

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;}

}

Page 61: Insertion Sort

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;}

}

Page 62: Insertion Sort

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;}

}

Page 63: Insertion Sort

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;}

}

Page 64: Insertion Sort

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;}

}

Page 65: Insertion Sort

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;}

}

Page 66: Insertion Sort

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;}

}

Page 67: Insertion Sort

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;}

}

Page 68: Insertion Sort

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;}

}

Page 69: Insertion Sort

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;}

}

Page 70: Insertion Sort

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;}

}

Page 71: Insertion Sort

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;}

}

Page 72: Insertion Sort

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;}

}

Page 73: Insertion Sort

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;}

}

Page 74: Insertion Sort

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;}

}

Page 75: Insertion Sort

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;}

}

Page 76: Insertion Sort

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;}

}

Page 77: Insertion Sort

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;}

}

Page 78: Insertion Sort

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;}

}

Page 79: Insertion Sort

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;}

}

Page 80: Insertion Sort

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;}

}

Page 81: Insertion Sort

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;}

}

Page 82: Insertion Sort

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;}

}

Page 83: Insertion Sort

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;}

}

Page 84: Insertion Sort

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;}

}

Page 85: Insertion Sort

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;}

}

Page 86: Insertion Sort

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;}

}

Page 87: Insertion Sort

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;}

}

Page 88: Insertion Sort

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;}

}

Page 89: Insertion Sort

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

The EndThe End