Você está na página 1de 34

SORTING

1
Definatión:
Sorting refers to the operation of arranging
data in some order such as increasing or
decreasing with numerical data
A={31621 34590 }
A={01123 34569 }

2
Review of Complexity
• The complexity of sorting is the function
measures the number of comparisons.
OR
• Time Complexity is defined to be the time the
computer takes to run a program (or algorithm
in our case).

3
Type of sorting

Bubble sort
 Selection sort
 Insertion sort
 Radix sort
 Quick sort

4
Bubble sort Algorithm

Data is an array with N element. T his algorithm sort the element is data
Step1: Repeat step 2 and 3 for k=1 to n-1
Step 2: set ptr :-=1[initializes pass pointer PTR.]
Step 3: repeat while PTR<N –K:[execute pass.]
(a)If DATA[PTR]>DATA[PTR+1]
Then
Interchange DATA[PTR] and DATA[PTR+1]
{end of if structure]
(b) Set PTR:=PTR+1
[end of inner loop]
[end of step 1 outer loop]
Step 4: exit
5
Example Bubble sort

10 13 14 29 37

6
selection sort Algorithm
Selection sort(A,N)
1.Repeat steps 2 and 3 for K=1,2,….,N-1:
2.Call MIN(A,K,N,LOC)
3.Interchange A[K] and A[LOC]
set
TEMP=A[K],A[K]=A[LOC],A[LOC]=TEMP
end of step 1 loop
4.Exit

7
Example selection sort

20 8 5 10 7

5 8 20 10 7

5 7 20 10 8

5 7 8 10 20

8
Insertion sort algorithm
Insertion sort(A,N)
1. Set A[0]= -
2. Repeat steps 3 to5 for K=2,3,…,N
3. Set TEMP=A[K] & PTR= K-1
4. Repeat while TEMP<A[PTR]
a. Set A[PTR+1]=A[PTR] (moves element forward)
b. Set PTR=PTR-1
End of loop
5. Set A[PTR+1]=TEMP(inserts element in proper place)
End of step 2 loop
6. Return.

9
Example Insertion sort

20 8 5 10 7

8 20 5 10 7

--- 8 20 5 10 7

10
Coding Bubble, insertion, Selection
sort
#include<stdio.h>
#include<stdlib.h>
#include "sorting.h"

main()

{
int array[] = {23, 78, 45, 8, 32, 56};
int size = 6;
int i;
char ans;

do
{
11
puts(" B=>Bubble sort");
puts("
S=>Selection sort");
puts("
I=>Insertion sort");
puts("
Q=>Quit");
printf("Please choose an
option to proceed:>");
scanf("%c", &ans);
fflush(stdin);
ans = tolower(ans);

switch(ans)
{
case 'b':
{

12
printf("The array before sorted is:
");

for(i=0;i<size;i++)
{

printf("%d ", array[i]);


}

bubbleSort(array, size);

puts("");

printf("The sorted array is:


");

for(i=0;i<size;i++)
{

13
print("%d ", array[i]);
}

puts("");

exit(101);
break;
}
case 's':
{

printf("The array before sorted is:


");

for(i=0;i<size;i++)
{

printf("%d ", array[i]);


}

14
selectionSort(array, size);

puts("");

printf("The array after sorted is:


");

for(i=0;i<size;i++)
{

printf("%d ", array[i]);


}

puts("");

exit(101);
break;
}

15
case 'i':
{

printf("The array before sorted is:


");

for(i=0;i<size;i++)

printf("%d ", array[i]);

insertionSort(array, size);

puts("");

printf("The array after sorted is:


");

for(i=0;i<size;i++)
{

printf("%d ", array[i]);


} 16
puts("");

exit(101);

break;
}
}
}while(an s!='q');

return 0;
}

17
Radix sort Algorithm

Radix sort is generalization of bucket sort.


It uses several passes of bucket sort
Perform the bucket sorts by “least significant
digits”
First sort by digit in units place
Second sort by digit in tens place
Third sort by digit in hundreds place

18
Example Radix Sort
First pass:

Second pass:

19
Third pass:

20
Radix sort coding

#define NUMELTS 100


# include<stdio.h>
#include<conio.h>
#include<math.h>
void radixsort(int a[],int);
void main()
{
int n,a[20],i;
clrscr();

printf(" enter the number :");


scanf("%d",&n);
printf(" ENTER THE DATA -");
for(i=0;i<n;i++)
{

21
printf("%d. ",i+1);
scanf("%d",&a[i]);
}
radixsort(a,n);
getch();
}
void radixsort(int a[],int n)
{
int rear[10],front[10],first,p,q,exp,k,i,y,j;
struct
{
int info;
int next;
}node[NUMELTS];
for(i=0;i<n-1;i++)
{

22
node[i].info=a[i];
node[i].next=i+1;
}
node[n-1].info=a[n-1];
node[n-1].next=-1;
first=0;

for(k=1;k<=2;k++) //consider only 2


digit number
{
for(i=0;i<10;i++)
{
front[i]=-1;
rear[i]=-1;
}

23
while(first!=-1)
{
p=first;
first=node[first].next;
y=node[p].info;
exp=pow(10,k-1);
j=(y/exp)%10;
q=rear[j];
if(q==-1)
front[j]=p;
else
node[q].next=p;
rear[j]=p;
}
for(j=0;j<10&&front[j]==-1;j++)
;
first=front[j];
while(j<=9)
{

24
for(i=j+1;i<10&&front[i]==-1;i++)
;
if(i<=9)
{
p=i;
node[rear[j]].next=front[i];
}
j=i;
}
node[rear[p]].next=-1;
}
//copy into original array
for(i=0;i<n;i++)
{
a[i]=node[first].info;
first=node[first].next;

25
clrscr();
textcolor(YELLOW);
printf(" DATA AFTER
SORTING:");
for(i=0;i<n;i++)
printf("%d . %d",i+1,a[i]);
}

26
Quick sort Algorithm
 To sort a[left...right]:
1. if left < right:
1.1. Partition a[left...right] such that:
all a[left...p-1] are less than
a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate

27
Example Quick sort
Input: 65 70 75 80 85 60 55 50 45
P: 65 i
Pass 1: 65 70 75 80 85 60 55 50 45
(i) i j  swap
(A[i], A[j])
65 45 75 80 85 60 55 50 70
(ii) i j  swap (A[i],
A[j])
65 45 50 80 85 60 55 75 70
(iii) i j  swap (A[i],
A[j])
65 45 50 55 85 60 80 75 70

(iv) i j  swap (A[i],


A[j])
65 45 50 55 60 85 80 75 70
(v) j i if (i>=j)
break

60 45 50 55 65 85 80 75 70

swap (A[left], A[j])

28
Continued……
60 45 50 55 65 85 80 75 70
Pass 2a (left sub-block): 60 45 50 55 (P = 60)
i j
60 45 50 55
j i if (i>=j) break
55 45 50 60 swap (A[left], A[j])

Pass 2b (right sub-block): 85 80 75 70 (P = 85)


i j
85 80 75 70
ji if (i>=j) break
70 80 75 85 swap (A[left], A[j])

29
Quick sort coding
#include<stdio.h>
#include<conio.h>
void qsort(int a[ ],int,int);
void partition(int a[ ],int,int,int *);
void print(int *,int);
void swap(int *,int *);

void main()
{
int a[30],i,n;
flushall();
clrscr();
printf("
enter how many data u want :");
scanf("%d",&n);

print(" enter the data :");


for(i=0;i<n ;I ++)

30
printf("
%d .",i);
scanf("%d",&a[i]);
}
qsort(a,0,n-1);
printf("
SORTED DATA:
");
print(a,n-1);
getch();
}
void qsort(int a[],int lb,int ub)
{
int j;
if(ub>lb)
{
partition(a,lb,ub,&j);
qsort(a,lb,j-2);
qsort(a,j+2,ub);
}
31
}
void partition(int a[],int lb,int ub,int *j)
{

int mid=(lb+ub)/2,temp,up,down,pivot;
pivot=a[mid];
up=ub;
down=lb;
while(down<up)
{
while( a[down] <= pivot && down <=
ub )
{
down++;
if(a[down]<a[down-1]&&down>lb)
swap(&a[down],&a[down-1]);
}
while(a[up]>pivot)
{

32
up--;
if(a[up]>a[up+1]&&up<ub)
swap(&a[up],&a[up+1]);
}
if(down<up)
{
swap(&a[down],&a[up]);
if(a[down]<a[down-1]&&down>0)
swap(&a[down],&a[down-1]);
if(a[up]>a[up+1]&&up<ub)
swap(&a[up],&a[up+1]);
}

}
for(int i=0;i<ub-1;i++)
if(a[i]>a[i+1])
swap(&a[i],&a[i+1]);
*j=up;
}
void print(int a[],int n)
{
33
for(int i=0;i<=n;i++)
printf("
%d . %d",i,a[i]);
}
void swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}

34

Você também pode gostar