Showing posts with label Sorting Method. Show all posts
Showing posts with label Sorting Method. Show all posts

Tuesday, 2 February 2016

Simplest way to understand Selection Sort

Suppose the we have set of cards, now we keep all the cards in our left hand and select one smallest card and then transfer that card to our right hand, similarly we repeat the process with all other cards then we will get all sorted cards in our right hand.

Below is pictorial representation of the example.
This is the set of cards that we have kept in our left hand.
Now we are transferring the smallest cards from our left hand to right hand one by one.

And thus now we come up with a sorted bunch of cards in our right hand.

You can apply this same approach in the programming. You have to notice thing that in the above example of "sorting of cards" we have used our two hands but in case of programming we have to use only one array.
We will see how we can use only a single array.
Now suppose that we have an array :
3,7,5,1,4,2
Now one way of sorting this can be selecting the smallest element from the array and strong it into another empty array and continuing the same procedure again and again. (But this will occupy extra memory space) So we will not follow this method.
Second approach:
We will look at the index of smallest element, and then we can swap that element with element of zero index. And again we can search of the minimum element and swap it with the element at index `1`. Repeat the same procedure and you will end up with a sorted array.

Algorithm of Selection Sort:

SelectinSort(Array, Size)
    for i <- 0 to n-2
        min = i
        for j = i+1 to n-1
            if Array[j] < Array[min]
                min = j
    swap Array[i] and Array[min]
Done
Program of Selection Sort:
This sorting is in ascending order.
#include<iostream>
void SelectionSort(int Array[], int size)
{
    int min, flag = 0; // flag is taken to check if the value of min changes or not
    for(int i = 0; i < size-2; i++)
    {
        min = i;
        for(int j = i+1; j < size-1; j++)
        {
            if(Array[j]<Array[min])
            {
                min = j;
                flag = 1;
            }
        }
        if(flag = 1)
        {
            int temp = Array[i];
            Array[i] = Array[min];
            Array[min] = temp;
        }
    }
    return;
}
int main()
{
    int Array[100];
    int Size;
    std::cout<<"\n Enter the number of elements you want to sort : ";
    std::cin>>Size;
    std::cout<<"\n Enter the elements of array : \n";
    for(int i = 0 ; i < Size; i++)
        std::cin>>Array[i];
    std::cout<<"\n Array before sorting is : \n";
    for(int i = 0 ; i < Size; i++)
        std::cout<<" "<<Array[i];
    SelectionSort(Array, Size);
    std::cout<<"\n The array after sorting is : \n";
    for(int i = 0 ; i < Size; i++)
        std::cout<<" "<<Array[i];
    return 0;
}

If you have any doubt then just comment below or send an email of youarefreak1@gmail.com.
Thanks.

Tuesday, 24 November 2015

Radix Sort in Python (in HINDI) | पाइथन में रेडिक्स सॉर्ट।

कथन:
प्राकृतिक क्रम में उन्हें पुनर्व्यवस्थित, पूर्णांकों की एक अव्यवस्थित सूची को देखते हुए।

नमूना इनपुट: [534, 246, 933, 127, 277, 321, 454, 565, 220]
नमूना आउटपुट: [127, 220, 246, 277, 321, 454, 534, 565, 933]

:समाधान की समय जटिलता (Time Complexity)
सबसे अच्छा मामले ओ (के एन) O(kn); औसत के मामले ओ (के एन)
O(kn); सबसे खराब स्थिति हे (के .एन.)O(kn),
जहाँ पर के (k) सबसे लम्बे नंबर की लम्बाई है।

k lon(n) "क लॉग (एन)" से अधिक है तो एक nlog (एन) एल्गोरिथ्म एक बेहतर फिट होगा। हकीकत में हम हमेशा मूलांक बदल सकते हैं

दृष्टिकोण:
मूलांक तरह, प्रकार और बाल्टी प्रकार की गिनती की तरह, आधारित एक पूर्णांक है
इनपुट सरणी के मूल्यों यानी # एल्गोरिथ्म (माना जाता है पूर्णांकों)। इसलिए मूलांक तरह सबसे तेजी से छँटाई एल्गोरिदम के बीच है चारों ओर, सिद्धांत में। मूलांक तरह के लिए विशेष भेद है
यह प्रत्येक सिफर (यानी अंकों) के लिए एक बाल्टी बनाता है कि #; जैसे की, प्रकार बाल्टी के लिए भी इसी तरह, मूलांक तरह प्रत्येक बाल्टी होना चाहिए अलग चाबियाँ स्वीकार कर सकते हैं कि # उगने वाली दाढ़ी सूची।
दशमलव मूल्यों के लिए , बकेट की संख्या दशमलव के रूप में 10 है, प्रणाली 10 अंकों के / Cyphers (यानी 0,1,2,3,4,5,6,7,8,9) है। फिर चाबियाँ लगातार महत्वपूर्ण अंक के अनुसार क्रमबद्ध हैं।

प्रोग्राम के नीचे लिखा है :-

def RadixSort(A):
    RADIX = 10
    maxLength = False
    tmp , placement = -1, 1

    while not maxLength:
        maxLength = True
        buckets = [list() for _ in range(RADIX)]
        for  i in A:
            tmp = i / placement
            buckets[tmp % RADIX].append(i)
            if maxLength and tmp > 0:
                maxLength = False

        a = 0
        for b in range(RADIX):
            buck = buckets[b]
            for i in buck:
                A[a] = i
                a += 1
    # move to next digit
    placement *= RADIX
    return A
A = [534, 246, 933, 127, 277, 321, 454, 565, 220]
print(RadixSort(A))



यही प्रोग्राम अगर आप सभी प्रकार के इनपुट के लिए लिखना चाहते हैं तो वह भी निचे लिखा गया है।
def RadixSort(A):
    RADIX = 10
    maxLength = False
    tmp , placement = -1, 1

    while not maxLength:
        maxLength = True
        buckets = [list() for _ in range(RADIX)]
        for  i in A:
            tmp = i / placement
            buckets[tmp % RADIX].append(i)
            if maxLength and tmp > 0:
                maxLength = False

        a = 0
        for b in range(RADIX):
            buck = buckets[b]
            for i in buck:
                A[a] = i
                a += 1
    # move to next digit
    placement *= RADIX
    return A
A = []
n = input("Enter the numebr of elements you want to sort : ")
print("Enter the numbers : \n")
for i in range(0, n):
    num = int(input())
    A.append(num)
print(RadixSort(A))



धन्यवाद