a new sorting algorithm (named 'u' sort) - Academic Journals Database

0 downloads 0 Views 50KB Size Report
algorithms, such as insertion sort, tend to have better performance than bubble sort. ... If the input array is reverse-sorted, insertion sort performs as many.
Upendra Singh Aswal et al. / International Journal of Engineering Science and Technology (IJEST)

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 do B[C[A[j]]] = A[j] C[A[j]] = C[A[J]] – 1

ISSN : 0975-5462

Vol. 3 No. 9 September 2011

7156

Upendra Singh Aswal et al. / International Journal of Engineering Science and Technology (IJEST)

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 mostused 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 of (n-1) + (n-2) + (n-3) + ………. + 1 = n * (n1)/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) Tn = (n-1)(n+3)

ISSN : 0975-5462

Vol. 3 No. 9 September 2011

7157

Upendra Singh Aswal et al. / International Journal of Engineering Science and Technology (IJEST)

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

Index:

1 2 3 4

Count :

2

0

2

0

3

5 6 7

8

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