Lecture 21:  Sorting 1 -- Selection

 

Objectives of this lecture

q       Introduce the concept of sorting

q       Lean the Selection Sorting Method.

 

What is Sorting?

q       Sorting is the re-arrangement of a collection of data according to some key-field.

q       It is a common activity in data management.  Even when a list is maintained in key order, there is often a need to re-arrange the list in a different order.

q       Because it takes so much processing time, sorting is a serious topic in computer science, about which many different algorithms have been written.

q       However, there is no perfect sorting algorithm as the efficiency of a sorting algorithm depends on many factors.  Among these are

Ø      Initial arrangement of the data; random, partially sorted, totally sorted, reverse-sorted

Ø      The data structure used: array, linked list, tree

Ø      Availability of memory and the size of the data

q       We shall consider some of these sorting methods in this and subsequent lectures.

 

Selection Sort:

q       This includes the following steps:

1.   Find the smallest (or largest) item in the list

2.   Place this item at the beginning of the list

3.   Repeat steps 1 and 2 starting at the beginning of the remaining list.

q       The following shows both iterative and recursive version of selection sort:

 

Iterative version:

void sel_sort1(int list[])

{  int min_pos, start_pos;

 

   for(start_pos=0;  start_pos<MAX_SIZE-1; start_pos++)

   {  min_pos=find_min_pos1(list, start_pos);

      swap1(list, start_pos, min_pos);

   }

}

 

Recursive version:

void sel_sort2(int list[], int start_pos)

{  int min_pos;

    if (start_pos<MAX_SIZE-1)

   {  min_pos=find_min_pos1(list, start_pos);

      swap1(list, start_pos, min_pos);

      sel_Sort2(list, start_pos+1);

   }

}

 

q       Both functions use the helper functions below:

 

int find_min_pos1(int list[], int start_pos)

{  int i, min_pos=start_pos;

  

   for (i=start_pos+1; i<MAX_SIZE; i++)

      if (list[i] < list[min_pos])

          min_pos = i;

  

   return min_pos;

}

 

void swap1(int list[], int pos1, int pos2)

{  int temp;

   temp=list[pos1];

   list[pos1]=list[pos2];

   list[pos2]=temp;

}

 

q       The following table shows a trace of how selection sort works:

 

Index

Original

Round1

Round3

Round 3

0

7

1

1

1

1

2

2

2

2

2

1

7

7

4

3

4

4

4

7

 

q       The following shows a version of the selection sort for data contained in a linked list with the following declarations:

 

typedef struct node_type {  ITEM_TYPE item;

                            struct node_type *next;

                         } NODE_TYPE;

typedef NODE_TYPE *NODE_PTR;

typedef NODE_TYPE *LIST_TYPE;

 

void sel_sort3 (LIST_TYPE *list)

{  NODE_PTR nim_pos, ptr=*list;

 

   while (ptr !=NULL)

   {  min_pos = find_min_pos2(ptr);

      swap2(ptr, min_pos);

      ptr = ptr->next;

   }

}

 

NODE_PTR find_min_pos2(NODE_PTR start)

{  NODE_PTR min_pos=start; ptr=start->next;

  

   while (ptr != NULL)

   {  if (min_pos->item.key > ptr->item.key)

         min_pos=ptr;

      ptr=ptr->next;

   }

   return (min_pos);

}

 

void swap2(NODE_PTR pos1, NODE_PTR pos2)

{  ITEM_TYPE temp;

   temp = pos1.item;

   pos1->item=pos2->item;

   pos2->item=temp;

}

 

Notes: 

q       Selection Sort has the advantage of simplicity and is adequate for small list of few hundred records.

q       The problem with selection sort is that the main loop executes n (number of records) times.  More over, in each iteration, it has to search the rest of the records for a minimum – about n comparisons.  Thus it needs approximately n*n steps.  For this reason, it is sometimes called a quadratic sorting method.

q       Another problem with selection sort is that it always scans the whole list looking for smallest even if the data happen to be in order.