Developing Algorithms Using Arrays

Developing Algorithms Using Arrays

Introduction to Algorithms with Arrays

  • An algorithm is a step-by-step procedure to solve a problem or achieve a specific outcome.
  • When arrays are involved in the development of algorithms, it generally involves processing each element of the array.

Traversing Arrays in Algorithms

  • One common algorithm pattern is traversing an array – generally, this involves visiting each element of the array once and processing it in some way.
  • To traverse an array means to examine each element once. This is usually done using a loop.
  • A typical example is finding the sum of every element in an array.

Searching in Arrays

  • A common application of algorithms for arrays is searchingfor an element.
  • Linear search involves iterating through the array from the start to the end.
  • Binary search involves repeatedly dividing in half the portion of the array that could contain an item, until narrowing it down to a single possible place.

Sorting Arrays

  • Arrays often need to be sorted into a certain order, and there are many algorithms to achieve this.
  • Bubble sort repeatedly steps through the array, compares adjacent elements and swaps them if they are in the wrong order. The pass through the array is repeated until the array is sorted.
  • Insertion sort builds a sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

Summary

  • Algorithms working with arrays will often involve loops to traverse the array.
  • The two most common uses of arrays in algorithms are searching and sorting.
  • Understanding the characteristics of different array algorithms is important in making efficient use of arrays. Knowing how to choose the most efficient algorithm for your needs can be a very useful skill.