• No results found

A NEW SORTING ALGORITHM (NAMED ‘U’ SORT)

N/A
N/A
Protected

Academic year: 2020

Share "A NEW SORTING ALGORITHM (NAMED ‘U’ SORT)"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

A NEW SORTING ALGORITHM

(NAMED ‘U’ SORT)

UPENDRA SINGH ASWAL1

Department of Computer Applications Graphic Era University, Dehradun, India

[email protected]

KAMLESH CHANDRA PUROHIT2

Department of Computer Applications Graphic Era University, Dehradun, India

[email protected]

SUJATA NEGI THAKUR3

Department of Computer Applications Graphic Era University, Dehradun, India

[email protected]

Abstract

Sorting is a technique which is frequently used in our day to day life. It has a direct implication on searching. If the data is sorted on any key attribute, finding data based on that key attribute becomes very fast. There are many sorting algorithm that are being used in practical life as well as in computation. We are going to introduce new sorting algorithm. Here algorithms are providing a solution to make it faster as compared to previous one. This paper presents a new sorting algorithm that belongs to O(N2) category for repeating elements and O(N)for non repeating elements in the array , here We have also discuss some of the previous sorting algorithm. Our work shows the implementation of this algorithm in C language. Our algorithm is found to be very simple and faster than other algorithms due to its unique implementation. Due to this reason the time and space complexities are significantly reduced.

Keywords: Sorting; counting sort; minimum; maximum; algorithm ,index ,order, complexity.

1. Introduction

Our new sorting technique is based on following counting sort ,We have taken the help from this sorting technique till the counting total number of elements of unsorted array and their index in Count array . The counting sort is explained as

Algorithm COUNTING SORT(array A, array B, int k) for i = 1 to k

do c[i] = 0 for j = 1 to length[A]

do C[A[j]] = C[A[j]] + 1

C[i] now contains the number of elements equal to i for i = 2 to k

do C[i] = C[i] + C[i-1]

/*C[i] now contains the number of elements less than or equal to i */ for j = length[A] down to 1

(2)

2. Related SortingAlgorithm And Their Complexity.

2.1 Selection sort :A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly[5]; it is also often useful for cannibalizing data and for producing human-readable output. More formally, the output must satisfy two conditions:

1.The output is in non decreasing order (each element is no smaller than the previous element according to the desired total order);

2.The output is a permutation, or reordering, of the input.

Complexity. Analysis of the straight selection sort is straightforward[1]. The first pass makes n-1 comparisons; the second pass makes n-2, and so on. Therefore, there is a total of1) + 2) + 3) + ………. + 1 = n * (n-1)/2. Comparisons, which is О(n2). The number of interchanges is always n-1 (unless a test is added to prevent the interchanging of an element with itself). There is little additional storage required (except to hold a few temporary variables). The sort may therefore be categorized as О(n2). The selection sort can be used for any file for which n is small.

2.2 Bubble sort Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort[5].Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large Bubble sort has worst-case and average complexity both О(n²), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n²) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore bubble sort is not a practical sorting algorithm when n is large.

Complexity Clearly for an array of size n, the outside loop repeats n-1 times. To begin with the inside loop does n-1 comparisons, next time n-2 and so on.

Finally on the last iteration of the outside loop, the inside loop does 1 comparison. So on average the inside loop does ((n-1) + 1) / 2 ≈ n/2 comparisons.

Therefore, the overall number of computation steps is n * n/2 = n2/2. Worst case performance O(n²)

Best case performance O(n) Average case performance O(n²) 2.3 Insertion sort.

Insertion sort is very similar to selection sort. As in selection sort, after k passes through the array[5], the first k elements are in sorted order. For selection sort these are the k smallest elements, while in insertion sort they are whatever the first k elements were in the unsorted array. Insertion sort's advantage is that it only scans as many elements as needed to determine the correct location of the k+1st element, while selection sort must scan all remaining elements to find the absolute smallest element. Calculations show that insertion sort will usually perform about half as many comparisons as selection sort. Assuming the k+1st element's rank is random, insertion sort will on average require shifting half of the previous k elements, while selection sort always requires scanning all unplaced elements. If the input array is reverse-sorted, insertion sort performs as many comparisons as selection sort. If the input array is already sorted, insertion sort performs as few as n-1 comparisons, thus making insertion sort more efficient when given sorted or "nearly-sorted" arrays.While insertion sort typically makes fewer comparisons than selection sort, it requires more writes because the inner loop can require shifting large sections of the sorted portion of the array. In general, insertion sort will write to the array O(n2) times, whereas selection sort will write only O(n) times. Step count for body of for loop is

Tn = 2(1+2+3+…+n-1) + 3(n-1) Tn = (n-1)n + 3(n-1)

(3)

Asymptotic Complexity of Insertion Sort O(n2).Time or number of operations does not exceed c.n2 on any input of size n (n suitably large).Actually, the worst-case time is Theta(n2) and the best-case is Theta(n)So, the worst-case time is expected to quadruple each time n is doubled

3. PROPOSED New sorting (U sort) ALGORITHM

3.1 Working

Suppose that the values to be sorted are all between 0 and k, w here k is some (small) integer[4]. Assume the unsorted values are in the array A[1..n].

Use an array Count [1…..k] to count how many times each key occurs in A. This requires O(n) time. Here K is any value as a difference of maximum and minimum values present in array A.

For example here K=5-0+1 =6 i.e total values in Count array are 6

A: 2 5 3 0 2 3 0 3

Index: 1 2 3 4 5 6 7 8

Count : 2 0 2 3 0 1

Element: 0 1 2 3 4 5

Here I have index which shows index no of each elements in the array A and Count array counts each element and show how many time the element is present in array A. ie 0, two times,1 zero time and so on.

Here complete counting sort is given below ,but I am taking only the above explain part from counting sort. Our complete algorithm with its implementation in ‘C’ language will be given next paragraph

The top of this paragraph illustrates a sub-subheading.

3.1 Complete U sort Algorithm :

Begin:

1. Array A[ ], B[ ],Count[ ];

Variables I, J, K, Min, Max, L=1, X=0

2. Read values into array A, Find Maximum and Minimum values from array A and store them into Max and Min variables.

3. Calculate K

K= Max – Min +1; 4. Initialize array Count to zero

For I =1 to K

Do Count[I]=0;

5. */Count array counts each element and show how many time the element is present in array A */ For J=1 to J<=Length of A

Do Count[A[J]]=Count[A[J]]+1;

6. /*Sorting of elements using the total count and index from the Count array*/ For I = 1 to K

IF (Count[I]!=0) X=Count[I];

For J=1 to X

/*only for repeating elements */ B[L]=I;

L=L+1 End For

End If End For

(4)

End Algorithm

3.3 ‘C’ Implementation of Algorithm

void main(){

int a[10],b[10],c[10],i,j,k=0; int min=0,max=0,l=1,x=0; clrscr();

printf("\nEnter any values");

for(i=1;i<=8;i++) /*Length of A is 8 */ scanf("%d",&a[i]);

printf("Plz enter Max and min value from the array A ") scanf("%d%d",&max,&min);

k=max-min+1;

for(i=1;i<=k;i++) c[i]=0;

for(j=1; j<=8; j++)

c[a[j]]=c[a[j]]+1; printf("\n C array is:"); for(i=1;i<=k;i++)

printf("%d",c[i]); for(i=1;i<=k;i++){

if(c[i]!=0) { x=c[i]; for(j=1;j<=x; j++){ b[l++]=i; }

} }

printf("\n The Sorted array is:") for(i=1;i<=8;i++

printf("%d",b[i]); getch();

}

4. PERFORMANCE EVALUATION

Analysis of the Upgraded selection sort is straightforward [1]. The first pass makes n-1 comparisons as the array is of length n; the second pass makes n - 3 comparisons as now the length of the current array has reduced to n-2, the third pass makes n-5 comparisons as the length of the current array is n-4 and so on. Therefore, there is a total of

(n-1) + (n-3) + (n-5) + ……….. + 1 = n * n

when Comparisons, which is О(N2) .But Our sorting algorithm for non repeating elements is of Order O(N). In the worst case , when there are repeating elemnts then it run in O(N2). There is little additional storage required (except to hold a few temporary variables). The sort may therefore be categorized as О(N). This sorting technique can be used for any file for which N is small.

Note that the Our sorting is stable: it preserves the ordering of elements that have the same key. (Previously seen sorting algorithms do not have this property, but some do have stable versions.)

5. CONCLUSION AND FUTURE WORK

(5)

Reference:

[1] Yedidyah Langsam,Moshe J. Augenstein, Aaron M. Tenenbaum. Data structure using C and C++ ,

[2] D.E. Kunth, “The Art of Computer Programming”, Vol. 1: Fundamental Algorithm, Addison-Wesley, Reading, Mass., 1973

[3] D.E. Kunth, “The Art of Computer Programming”, Vol. 3: Sorting and Searching, Addison-Wesley, Reading, Mass., 1973

[4] http://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/sortingII/countingSort/count.html

[5] http://en.wikipedia.org/wiki/Sorting_algorithm

The number of l

Th

e time co

mp

lex

ity

1 2 3

4

8

1

2

1

6

2

0

O (n2) (Existing algorithms)

O (n) (Proposed algorithm)

References

Related documents

Insert the blood collection tube into the holder and onto the needle up to the guideline on the needle holder.. Remove sheath

a Sagittal T2 image demonstrates short segment hyperintensity (bracket) with slight expansion of the cervical cord b Axial T2 image demon- strates greater than 2/3 cord in-

GADPH: Glyceraldehyde-3-phosphate dehydrogenase; ICU: Intensive care unit; MHC: Myosin heavy chain; MIP: Maximal inspiratory pressure; MOS: Mitochondrial oxidative stress;

The financial reporting scandals that was recorded in Enron, Cadbury Nigeria and some banks like Oceanic bank, International bank and Afribank has prompted researchers and

While Latour’s philosophy of social science demands an absolute abandonment of theory in all its forms, proposing instead to simply ‘go on describing’, he is in

In mice subjected to PSNL, repeated once daily intrathecal injection of TAT-GESV (10 nmol i.t.) and MK-801 (5 nmol i.t.) across eight consecutive days elevated (A) mechanical

the land. In the 1920s and 1930s, land grabbing was prevalent in agricultural regions in the Philippines. The refusal of tenants to vacate the land could be traced, in some

&#34;Our development is going in the right direction and we showed already last year that we can play equal with teams in the top division such as Sweden, Russia and Germany,&#34;