SlideShare une entreprise Scribd logo
1  sur  44
Télécharger pour lire hors ligne
Computer Algorithms .
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
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).
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
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 .
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.
What Kinds of problems are solved by
algorithms?
●    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.
What Kinds of problems are solved by
algorithms?
● 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.
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 syntax's rules and elegant design
  make it readable even among multiprogrammer
  development teams.
Advantages of "Python" as a
Programming Language :
Readability :

● Python's 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
Advantages of "Python" as a
Programming 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
Advantages of "Python" as a
Programming 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.
Advantages of "Python" as a
Programming 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.
Advantages of "Python" as a
Programming Language :
Portability :

● Besides running on multiple systems, Python
  has the same interface on multiple
  platforms. Its design isn't 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.
Advantages of "Python" as a
Programming Language :
Object-Oriented Programming :

● Some of the implemented OO functionality in
  Python is inheritance and polymorphism.
Examples of "Python based Solutions" for

     different "Computer Algorithms   " ..
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.
Linear Search :
● Known as sequential search
● is a method for finding a particular
   value in alist,
   that consists of checking every one of
its     elements,
  one at a time and in sequence, until the
desired one is found.
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
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.
Best, Worst and Average
Cases :
● Worst case :-     O(n)


● Average case :-     O(n).
binary search :

●       Suppose DATA ia an array which i
    stored in increasing numerical order or
    alphabetically.

     Then There are an extremely efficient
searching algorithm , called binary search,
     That can be used to find the location LOC
of a given ITEM of information in DATA.
Binary search :
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
Binary Search :


 if data[mid]==item:
    loc=mid
 else :
    loc=-1
 return loc
Best, Worst and Average
cases :
● Worst case :-     O(log2 n).

● Average case :-     O(log2 n).
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)
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.
Limitations of the Binary Search
Algorithm :
●    -The Binary Search Algorithm requires
    two conditions:

(1) the list must be sorted.

(2) direct access to the middle element in any
sublist.
Limitations of the Binary Search
Algorithm :
● 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
Sorting :

● Sorting : is the process of rearranging the
  data in array or list in insreasing or
  decreasing order .
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 .
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.
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
Best, Worst and Average
Cases :
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) .
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 .
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
Best, Worst and Average
Cases :

● Best case :    O(n2) .

● Worst case :     O(n2) .

● Average case :      O(n2) .
Merge Sort :
● Merge sort : a divide and conquer algorithm
  that was invented by John von Neumann in
  1945.
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 )
Best, Worst and Average
Cases :
● Best case :      O(n log n) .

● Worst case :      O(n log n) .

● Average case :       O(n log n) .

Contenu connexe

Tendances

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structuresKuber Chandra
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsMohamed Loey
 

Tendances (20)

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Red black tree
Red black treeRed black tree
Red black tree
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Red black tree
Red black treeRed black tree
Red black tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
ARIES Recovery Algorithms
ARIES Recovery AlgorithmsARIES Recovery Algorithms
ARIES Recovery Algorithms
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 

En vedette

Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5Kumar
 
Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...
Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...
Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...Lucidworks
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection TechniquesAn Khuong
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data StructureTalha Shaikh
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 

En vedette (11)

Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 
Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...
Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...
Native Code & Off-Heap Data Structures for Solr: Presented by Yonik Seeley, H...
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
Binary search
Binary search Binary search
Binary search
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Binary search
Binary searchBinary search
Binary search
 
Link List
Link ListLink List
Link List
 
Decision trees
Decision treesDecision trees
Decision trees
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 

Similaire à Computer Algorithms and Python Solutions

jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfVinayNassa3
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures NotesRobinRohit2
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingMvenkatarao
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure Eman magdy
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.pptArumugam90
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Salman Qamar
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introductionbabuk110
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...DrkhanchanaR
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 

Similaire à Computer Algorithms and Python Solutions (20)

Iare ds ppt_3
Iare ds ppt_3Iare ds ppt_3
Iare ds ppt_3
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures Notes
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures  Design-Notes-Searching-Hashing.pdfAD3251-Data Structures  Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
 
Chapter two
Chapter twoChapter two
Chapter two
 
Algorithms
Algorithms Algorithms
Algorithms
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 

Plus de Faculty of Science , portsaid Univeristy (8)

Library Management System
Library Management SystemLibrary Management System
Library Management System
 
compiler vs interpreter
compiler vs interpretercompiler vs interpreter
compiler vs interpreter
 
الحسن بن الهيثم
الحسن بن الهيثمالحسن بن الهيثم
الحسن بن الهيثم
 
الحسن بن الهيثم
الحسن بن الهيثمالحسن بن الهيثم
الحسن بن الهيثم
 
Html course
Html courseHtml course
Html course
 
Teach yourself html_ar
Teach yourself html_arTeach yourself html_ar
Teach yourself html_ar
 
GUI
GUI GUI
GUI
 
Algorithms python arraylistmatrix
Algorithms python arraylistmatrixAlgorithms python arraylistmatrix
Algorithms python arraylistmatrix
 

Dernier

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Dernier (20)

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Computer Algorithms and Python Solutions

  • 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. 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. 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. 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. 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. What Kinds of problems are solved by algorithms? ● 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. What Kinds of problems are solved by algorithms? ● 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. 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 syntax's rules and elegant design make it readable even among multiprogrammer development teams.
  • 10. Advantages of "Python" as a Programming Language : Readability : ● Python's 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. Advantages of "Python" as a Programming 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. Advantages of "Python" as a Programming 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. Advantages of "Python" as a Programming 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. Advantages of "Python" as a Programming Language : Portability : ● Besides running on multiple systems, Python has the same interface on multiple platforms. Its design isn't 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. Advantages of "Python" as a Programming Language : Object-Oriented Programming : ● Some of the implemented OO functionality in Python is inheritance and polymorphism.
  • 16. Examples of "Python based Solutions" for different "Computer Algorithms " ..
  • 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. Linear Search : ● Known as sequential search ● is a method for finding a particular value in alist, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.
  • 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. 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. Best, Worst and Average Cases : ● Worst case :- O(n) ● Average case :- O(n).
  • 22. binary search : ● Suppose DATA ia an array which i stored in increasing numerical order or alphabetically. Then There are an extremely efficient searching algorithm , called binary search, That can be used to find the location LOC of a given ITEM of information in DATA.
  • 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. Binary Search : if data[mid]==item: loc=mid else : loc=-1 return loc
  • 26. Best, Worst and Average cases : ● Worst case :- O(log2 n). ● Average case :- O(log2 n).
  • 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. 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. Limitations of the Binary Search Algorithm : ● -The Binary Search Algorithm requires two conditions: (1) the list must be sorted. (2) direct access to the middle element in any sublist.
  • 30. Limitations of the Binary Search Algorithm : ● 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. Sorting : ● Sorting : is the process of rearranging the data in array or list in insreasing or decreasing order .
  • 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. 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.
  • 35. 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
  • 36. Best, Worst and Average Cases : 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) .
  • 37. 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 .
  • 38.
  • 39. 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
  • 40. Best, Worst and Average Cases : ● Best case : O(n2) . ● Worst case : O(n2) . ● Average case : O(n2) .
  • 41. Merge Sort : ● Merge sort : a divide and conquer algorithm that was invented by John von Neumann in 1945.
  • 42. 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 )
  • 43.
  • 44. Best, Worst and Average Cases : ● Best case : O(n log n) . ● Worst case : O(n log n) . ● Average case : O(n log n) .