Objectives of this lecture
What is Sorting?
Selection Sort:
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);
}
}
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);
}
}
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;
}
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 |
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;
}