Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Algorithms with python .

Published in: Education, Technology
  • Be the first to comment


  1. 1. Computer Algorithms .
  2. 2. Algorithm● the Latin form of Al-Khw?rizm .● Algorithm:is well-defined computation procedure that takes some value,or set of values, as input and produces some value , or set of values,as output.● a sequence of computational steps that transfer the input into the output● a tool for solving a well-specified computional problem
  3. 3. Example of Algorithms :● when you need to sort asequence of numbers into nondecreasing order (31,42,59,26,58) Input:A sequence of n numbers(instance of program) (31,42,59,26,58). Output:A reordering of the input sequence (26,31,42,58,59).
  4. 4. Solve this problem with python: def sort(a,n): i=0 for i in range(n): min=i m=i+1 for m in range(n): if a[m]<a[min]: min=m temp=a[i] a[i]=a[min] a[min]=temp
  5. 5. Computer algorithms :● in computer systems, an algorithm is basically an instance of logic written in software by software developers to be effective for the target computer, in order for the target machines to produce output from given input .
  6. 6. Why computer algorithms are important ? ● The topic of algorithms is important in computer science because it allows for analysis on different ways to compute things and ultimately come up with the best way to solve a particular problem. By best I mean one that consumes the least amount of resources or has the fastest running time.
  7. 7. What Kinds of problems are solved byalgorithms?● The Human Genome Project : made a great progress toward the goals of identifying all the 100.000 genes in human DNA determining the sequences of the 3 billion chemical base pairs that make up human DNA ,sorting the information in databases,and developing tools for data analysis.Each of these steps requires algorithms.
  8. 8. What Kinds of problems are solved byalgorithms?● The Internet enables people around the world to quick access and retrieve large amounts of information.● Electronic commerce enables goods and services to be exchanged electronically.● Manufacturing and other commerical enterprises need to allocate rescources in the most beneficial way.
  9. 9. Advantages of "Python" as a Programming Language● Python is an interpreted, high-level programming language, pure object-oriented, and powerful server-side scripting language for the Web. Like all scripting languages, Python code resembles pseudo code. Its syntaxs rules and elegant design make it readable even among multiprogrammer development teams.
  10. 10. Advantages of "Python" as aProgramming Language :Readability :● Pythons syntax is clear and readable.● Python has fewer "dialects" than other l languages, such as Perl. And because the block structures in Python are defined by indentations
  11. 11. Advantages of "Python" as aProgramming Language :It Is Simple to Get Support :● Python community always provides support to Python users.● Python code is freely available for everyone.● many people are creating new enhancements to the language and sending them for approval
  12. 12. Advantages of "Python" as aProgramming Language :Fast to Learm .Fast to Code :● Python provides fast feedback in several ways.● Python provides a bottom-up development style in which you can build your applications by importing and testing critical functions in the interpreter before you write the top-level code that calls the functions.
  13. 13. Advantages of "Python" as aProgramming Language :Reusability :● You can easily share functionality between your programs by breaking the programs into modules, and reusing the modules as components of other programs.
  14. 14. Advantages of "Python" as aProgramming Language :Portability :● Besides running on multiple systems, Python has the same interface on multiple platforms. Its design isnt attached to a specific operational system because it is written in portable ANSI C. This means that you can write a Python program on a Mac, test it using a Linux environment, and upload it to a Windows server.
  15. 15. Advantages of "Python" as aProgramming Language :Object-Oriented Programming :● Some of the implemented OO functionality in Python is inheritance and polymorphism.
  16. 16. Examples of "Python based Solutions" for different "Computer Algorithms " ..
  17. 17. SEARCHING :● The operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM does not appear there.● There are many differtent serarching algorithms : The algorithm deponds on the way the information in DATA is organized.
  18. 18. Linear Search :● Known as sequential search● is a method for finding a particular value in alist, that consists of checking every one ofits elements, one at a time and in sequence, until thedesired one is found.
  19. 19. linear search: #linear search in python:def linear_search (a,n,item): k=0 for k in range(n): if item ==a[k]: loc=k return loc
  20. 20. the disadvantages of linear search : ● in linear search it needs more space and time complexity. ● in linear search if the key element is the last element and the search is from first element that is a worst case, if the key element is the first element and the search is from last element then also is the worst case.
  21. 21. Best, Worst and AverageCases :● Worst case :- O(n)● Average case :- O(n).
  22. 22. binary search :● Suppose DATA ia an array which i stored in increasing numerical order or alphabetically. Then There are an extremely efficientsearching algorithm , called binary search, That can be used to find the location LOCof a given ITEM of information in DATA.
  23. 23. Binary search :
  24. 24. Binary Search : def binary_search(data,n,lb,ub,item): beg=lb end=up mid=(beg+end)/2 while beg<=end and data[mid]!=item: if item<data[mid]: end=mid-1 else: beg=mid+1 mid=(beg+end)/2
  25. 25. Binary Search : if data[mid]==item: loc=mid else : loc=-1 return loc
  26. 26. Best, Worst and Averagecases :● Worst case :- O(log2 n).● Average case :- O(log2 n).
  27. 27. The Binary Search Algorithm works as follows :● During each stage of our algorithm, our search for ITEM is reduced to a segment of element of DATA: DATA[BEG],DATA[BEG+1],............., DATA[END]● The algorithm compares ITEM with the middle element DATA[MID] of the segment MID=((BEG+END)/2)
  28. 28. The Binary Search Algorithm works as follows :● If ITEM < DATA[MID], then ITEM can appear only in the left half of the segment: DATA[BEG],DATA[BEG+1],.............,DATA[MID-1]-reset END=MID-1.● If ITEM > DATA[MID], then ITEM can appear only in the right half of the segment::DATA[MID+1],DATA[MID+2],...........,DATA[END].-reset BEG=MID+1.
  29. 29. Limitations of the Binary SearchAlgorithm :● -The Binary Search Algorithm requires two conditions:(1) the list must be sorted.(2) direct access to the middle element in anysublist.
  30. 30. Limitations of the Binary SearchAlgorithm :● keeping data in sorted array is normally very expensive when there are many insertions and deletions.● Thus one may use a different data structure such as linked list or a binary search tree
  31. 31. Sorting :● Sorting : is the process of rearranging the data in array or list in insreasing or decreasing order .
  32. 32. Insertion Sort :● Simple sorting algorithim .● Ii is very popular when first sorting .● Efficient for sorting small data .(more efficient than bubble and selection sort)● Slow for sorting large data, thus Time can be saved by performing a binary search to find the location in which insert data[i] in the in input array .● Online : can sort input array as it recevied .
  33. 33. Insertion Sort :● insertion sort works as follows : ■ removes an element from the input data, inserting it into the correct position , to find the correct position we compare this element with each element in the input array, from right to left, until no input elements remain.
  34. 34. Insertion Sorting :def InsertionSort(data,n): for j in range(1,n): key = data[j] i=j-1 while (data[i] > key): data[i+1] = data[i] i=i-1 data[i+1] = key
  35. 35. Best, Worst and AverageCases :The Best Case: in which the input array was already sorted .This gives Inserting Sort linear running time O(n).● The Worst Case : in which the input array was sorted in reverse order .This gives Inserting Sort quadratic running time O(n2).● The Average Case : O(n2) .
  36. 36. Selection Sort :● Selection sort works as follows : ● Find the location loc of the smallest element in input array and put it in the first position, find the location loc of the second smallest element and put it in the second position, And so on .
  37. 37. Selection Sort : def sort(a,n): i=0 for i in range(n): min=i m=i+1 for m in range(n): if a[m]<a[min]: min=m temp=a[i] a[i]=a[min] a[min]=temp
  38. 38. Best, Worst and AverageCases :● Best case : O(n2) .● Worst case : O(n2) .● Average case : O(n2) .
  39. 39. Merge Sort :● Merge sort : a divide and conquer algorithm that was invented by John von Neumann in 1945.
  40. 40. Merge Sort :● Merge Sort works as follows : ● Divide the unsorted list into n sublists, repeatedly merge sublists to produce new sublists until each sublist contain one element . (list of one element consider sorted )
  41. 41. Best, Worst and AverageCases :● Best case : O(n log n) .● Worst case : O(n log n) .● Average case : O(n log n) .