Anda di halaman 1dari 5

Sorting

We encounter several applications that require an ordered list. So it is required to order the elements of a given
list either in ascending/increasing order or descending/decreasing order, as per the requirement. This process
is called sorting. There are many techniques available for sorting an array-based list.

Selection Sort
We perform a search through the table starting from the first record to locate the element with the smallest
key, when this record is found were interchange it with the first record in the table as a result of this
interchange the smallest key is placed in the first position of the table. In the second time or iteration we
locate second smallest key examining the key s of the records staring from the second record onwards.
/*In this program we use selection sorting method min=j;
to sorting array elements.*/ }
#include<stdio.h> }
#include<conio.h> if(min!=i)
void main() {
{ t=a[i];
int a[20],i,j,n,t,min,m; a[i]=a[min];
clrscr(); a[min]=t;
printf("enter how many elements"); }
scanf("%d",&n); printf("\n\n%dpass is : ",i);
printf("enter elements"); for(m=1;m<=n;m++)
for(i=1;i<=n;i++) {
{ printf("\t%d",a[m]);
scanf("%d",&a[i]); }
} }
for(i=1;i<=n-1;i++) printf("\n\nAfter sorting result is :\n\n");
{ for(i=1;i<=n;i++)
min=i; {
for(j=i+1;j<=n;j++) printf("\t%d",a[i]);
{ }
if(a[min]>a[j]) getch();
{ }

Bubble Sort
The bubble sort gets its name from the way smaller entries "sink" to the bottom of the array during the sort
while larger entries "bubble up" to the top. To carry out the bubble sort on the array traverse the array,
comparing two adjacent elements, starting with the first two, and ending with the last two. If the [i + 1]th
element value exceeds the ith, interchange the two element values. When a traversal is completed, it is
called a pass through the array. Continue making passes until a pass occurs in which no interchanges are
required.

Algorithm
1.Begin
2 Read the n elements
3. for I=1 to n
for j=n down to I+1
if a[j]<=a[j-1]
swap(a[j],a[j-1])
4. End
/*In this program we will sorting with bubble {
sort.*/ t=a[j];
#include<stdio.h> a[j]=a[j+1];
#include<conio.h> a[j+1]=t;
void main() }
{ }
int a[20],i,j,n,t,m; printf("\n\n%dpass is : ",i);
clrscr(); for(m=1;m<=n;m++)
printf("enter how many elements"); {
scanf("%d",&n); printf("\t%d",a[m]);
printf("enter elements"); }
for(i=1;i<=n;i++) }
{ printf("\n\nAfter sorting result is :\n\n");
scanf("%d",&a[i]); for(i=1;i<=n;i++)
} {
for(i=1;i<=n;i++) printf("\t%d",a[i]);
{ }
for(j=1;j<=n-i;j++) getch();
{ }
if(a[j]>a[j+1])

Quick Sort
The basis of quick sort is the divide and conquer strategy i.e divide the problem [list to be sorted]into sub
problems [sub lists] until solved sub problems [sorted sub lists] are found .

Divide: The array A[p . . r] is partitioned (rearranged) into two nonempty subarrays A[p . . q] and A[q + 1 . .
r] such that each element of A[p . . q] is less than or equal to each element of A[q + 1 . . r]. The index q is
computed as part of this partitioning procedure.

Conquer: The two subarrays A[p . . q] and A[q + 1 . . r] are sorted by recursive calls to quicksort.

Combine: Since the subarrays are sorted in place, no work is needed to combine them: the entire array A[p .
. r] is now sorted

Insertion Sort

The insertion sort works just like its name suggests - it inserts each item into its proper place in the final list.
The simplest implementation of this requires two list structures - the source list and the list into which sorted
items are inserted. To save memory, most implementations use an in-place sort that works by moving the
current item past the already sorted items and repeatedly swapping it with the preceding item until it is in
place.

This is a naturally occurring sorting method exemplified by a card player arranging the cards dealt to him. H
e picks up the cards as they are dealt and inserts them into the required position. Thus at every step we insert
an item into its proper place in an already ordered list.

The basic step in this method is to insert a record R into a sequence of ordered records, R1,R2, ...,Ri, (K1
K2, ..., Ki) in such a way that the resulting sequence of size i + 1 is also ordered. The algorithm below
accomplishes this insertion. It assumes the existence of an artificial record Ro with key Ko = - (i.e., all keys
are Ko).
procedure INSERT (R,i)
//Insert record R with key K into the ordered sequence Ro, ...,Ri
in such a way that the resulting sequence is also ordered on key
K. We assume that Ro is a record (maybe a dummy) such that
K Ko//
j i
while K < Kj do
//move Rj one space up as R is to be inserted left of Rj//
Rj+1 Rj; j j - 1
end
Rj+1 R
end INSERT

/*In this program we use insertion sorting method if use greater than(>) than sorting
to sorting array elements.*/ in descending order*/
#include<stdio.h> {
#include<conio.h> t=a[i];
void main() a[i]=a[j];
{ a[j]=t;
int a[20],i,j,n,t,min,m; }
clrscr(); }
printf("enter how many elements"); printf("\n\n%dpass is : ",i);
scanf("%d",&n); for(m=1;m<=n;m++)
printf("enter elements"); {
for(i=1;i<=n;i++) printf("\t%d",a[m]);
{ }
scanf("%d",&a[i]); }
} printf("\n\nAfter sorting result
for(i=1;i<=n;i++) is :\n\n");
{ for(i=1;i<=n;i++)
for(j=1;j<=i;j++) {
{ printf("\t%d",a[i]);
if(a[i]<a[j])/*if use less than(<) }
sign than sorting in ascending order getch();
}

Merging

The merge sort splits the list to be sorted into two equal halves, and places them in separate arrays.
Each array is recursively sorted, and then merged back together to form the final sorted list.The
fundamental operation in this algorithm is merging two sorted lists. Because the lists are sorted, this can be
done in one pass through the input, if the output is put in a third list. The basic merging algorithm takes two
input arrays a and b, an output array c, and three counters, aptr, bptr, and cptr, which are initially set to the
beginning of their respective arrays. The smaller of a[aptr] and b[bptr] is copied to the next entry in c, and
the appropriate counters are advanced. When either input list is exhausted, the remainder of the other list is
copied to c. An example of how the merge routine works is provided for the following input.
If the array a contains 1, 13, 24, 26, and b contains 2, 15, 27, 38, then the algorithm proceeds as follows:
First, a comparison is done between 1 and 2. 1 is added to c, and then 13 and 2 are compared.

2 is added to c, and then 13 and 15 are compared.

Elementary implementations of the merge sort make use of three arrays - one for each half of the data set
and one to store the sorted list in. The below algorithm merges the arrays in-place, so only two arrays are
required.

#include <stdlib.h>
#include <stdio.h> void m_sort(int numbers[], int temp[], int left, int
right)
#define NUM_ITEMS 10 {
int mid;
void mergeSort(int numbers[], int temp[], int
array_size); if (right > left)
void m_sort(int numbers[], int temp[], int left, int {
right); mid = (right + left) / 2;
void merge(int numbers[], int temp[], int left, int m_sort(numbers, temp, left, mid);
mid, int right); m_sort(numbers, temp, mid+1, right);

int numbers[NUM_ITEMS]; merge(numbers, temp, left, mid+1, right);


int temp[NUM_ITEMS]; }
}

int main()
{ void merge(int numbers[], int temp[], int left, int
int i; mid, int right)
clrscr(); {
printf("\nEnter element="); int i, left_end, num_elements, tmp_pos;
for (i = 0; i < NUM_ITEMS; i++)
scanf("%d",&numbers[i]); left_end = mid - 1;
//perform merge sort on array tmp_pos = left;
mergeSort(numbers, temp, NUM_ITEMS); num_elements = right - left + 1;
printf("Done with sort.\n");
for (i = 0; i < NUM_ITEMS; i++) while ((left <= left_end) && (mid <= right))
printf("%i\n", numbers[i]); {
getch(); if (numbers[left] <= numbers[mid])
} {
temp[tmp_pos] = numbers[left];
tmp_pos = tmp_pos + 1;
void mergeSort(int numbers[], int temp[], int left = left +1;
array_size) }
{ else
m_sort(numbers, temp, 0, array_size - 1); {
} temp[tmp_pos] = numbers[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
} temp[tmp_pos] = numbers[mid];
} mid = mid + 1;
tmp_pos = tmp_pos + 1;
while (left <= left_end) }
{
temp[tmp_pos] = numbers[left]; for (i=0; i <= num_elements; i++)
left = left + 1; {
tmp_pos = tmp_pos + 1; numbers[right] = temp[right];
} right = right - 1;
while (mid <= right) }
{ }

Anda mungkin juga menyukai