Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Quick sort in python

 Quick sort also use divide and conquer strategy.

In this we find out the correct position for the pivot element and partition the list into two this method is called partition method. Then further seperate the list as same. This method is the backbone of the quick sort.

Using pivot element we going to sort the list. We should follow few condition for quick sort

1) pivot element

2) if start is less than pivot increment

3) if end is greater than pivot increment

[---lesser num--, pivot, --- greater num---] 

Number less than pivot should be in left and greater should be in right as above format.

We see more clearly in a example

Algorithm:

Example:

Take list[5, 16, 23, 45, 61, 34, 4]

Take 5 as pivot element

pivot = 5
start = list[o]=5
End = list[6]=4

Now start comparing from start and end

i) if start is less than or equal pivot increment

ii) if end is great than pivot increment

iii) when both condition false then swap

-5 is less than or equal pivot, yes so increment
-16 is less than pivot, no so stop
-now check end 4 greater than pivot, no so stop

Now both the condition are false so swap (16,4) follow same for other elements as shown in below.

Quick sort in python



Quick sort in python
                                           

Quick sort in python

                                                                      10 > pivot so stop

Quick sort in python

                                                                    Now check end

Quick sort in python

Quick sort in python

swap(10,2)

Quick sort in python



Quick sort in python




Quick sort in python


Quick sort in python



Quick sort in python

                        start and end intersected or end less than start swap(end,pivot)
                                                               (5,7)

Quick sort in python


 
Quick sort in python


Take [5, 6, 2] as list1 and [9 , 10, 15, 8] as list follow same method for list1 and list2.

[5,6,2]

Pivot = 5
Quick sort in python

6 > 5 and 2 < 5 so swap(6,2)

Quick sort in python


Quick sort in python


End and start intersected so swap pivot and end
Quick sort in python


 
When you follow same step for list2 at last you get......
Quick sort in python
Now combine all the sublist to get a sorted list

Quick sort in python
Code  for 
Quick sort:
def partition(listlowhigh):
    i = (low-1)         
    pivot = list[high]     
  
    for j in range(low, high):
  
        
        if list[j] <= pivot:
  
            
            i = i+1
            list[i], list[j] = list[j], list[i]
  
    list[i+1], list[high] = list[high], list[i+1]
    return (i+1)
  

  
  
def quickSort(listlowhigh):
    if len(list) == 1:
        return list
    if low < high:
  
        
        pi = partition(list, low, high)
  
        
        quickSort(list, low, pi-1)
        quickSort(list, pi+1, high)
  
  
# Driver code to test above
list = [1078915]
n = len(list)
print("before sorting:",list)
quickSort(list0, n-1)
print("Sorted array is:")
for i in range(n):
    print("%d" % list[i]),
print("after sorting:",list)
  


Output:
before sorting: [10, 7, 8, 9, 1, 5] Sorted array is: 1 5 7 8 9 10 after sorting: [1, 5, 7, 8, 9, 10]
  


Hope you understand the program and try yourself for more clear view ,feel free to comment your thoughts!!






Comments

Popular Posts