The document discusses the array data structure and the bubble sort algorithm. Key points:
- An array is a linear data structure that stores elements in contiguous memory locations. It is defined by its size and elements can be accessed via indices.
- Bubble sort works by repeatedly "bubbling up" the largest value to its sorted position. It compares adjacent elements and swaps them if out of order.
- The bubble sort algorithm takes O(n^2) time since in the worst case, each of the n passes could involve n-1 swaps. It is one of the simplest sorting algorithms but has poor time complexity.
The document discusses different data structures and sorting algorithms:
1. It describes arrays as a linear data structure where elements are stored in consecutive memory locations and can be accessed using an index.
2. It provides examples of traversing and manipulating arrays, such as inserting and deleting elements. Common sorting algorithms like bubble sort and selection sort are also summarized.
3. Bubble sort works by repeatedly stepping through the list and comparing adjacent elements, swapping them if they are in the wrong order until no swaps are needed, resulting in a sorted list in O(n^2) time. Selection sort finds the minimum element and swaps it into the front each iteration.
PPT Lecture 2.2.1 onn c++ data structuresmidtushar
This document provides information about an Elementary Data Structure Using C++ course. The course objectives are to enable students to understand C++ programming constructs, analyze and solve problems in C++, understand data structures concepts, and analyze algorithm efficiency. The course covers arrays, linear data structures, searching, sorting, and traversal algorithms. Key concepts discussed include linear arrays, multi-dimensional arrays, array implementation in memory, insertion and deletion in arrays, linear and binary search algorithms, and applications of arrays in sorting, matrix operations, and CPU scheduling.
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
1) The document discusses linear arrays and their representation in memory. A linear array stores elements in consecutive memory locations.
2) The number of elements in an array is given by the length formula: Length = Upper Bound - Lower Bound + 1. The address of each element can be calculated using the base address of the array.
3) Common array operations like traversing, inserting, and deleting elements are discussed along with algorithms to perform each operation. Representing polynomials and sparse matrices using arrays is also covered.
1. The document discusses various data structures concepts including arrays, dynamic arrays, operations on arrays like traversing, insertion, deletion, sorting, and searching.
2. It provides examples of declaring and initializing arrays, as well as dynamic array allocation using pointers and new/delete operators.
3. Searching techniques like linear search and binary search are explained, with linear search comparing each element sequentially while binary search eliminates half the elements at each step for sorted arrays.
The document summarizes various sorting algorithms:
- Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. It requires O(n^2) time.
- Insertion sort iterates through the list and inserts each element into its sorted position. It is an adaptive algorithm with O(n) time for nearly sorted inputs.
- Quicksort uses a divide and conquer approach, recursively partitioning the list around a pivot element and sorting the sublists. It has average case performance of O(nlogn) time.
This document contains information about arrays and algorithms for traversing, inserting, and deleting elements from a linear array. It defines an array as a list of homogeneous data elements referenced by consecutive integer indices and stored in successive memory locations. It provides pseudocode for algorithms to traverse a linear array by applying an operation to each element, insert a new element into a specified position by moving other elements, and delete an element from a specified position by moving elements into the freed space.
This document discusses linear arrays and algorithms for common operations on arrays such as traversing, inserting, deleting, and sorting elements. It defines a linear array as a list of homogeneous data elements with a lower and upper bound. Algorithms provided include traversing from the lower to upper bound, inserting an element at a specified position by shifting other elements and incrementing the size, deleting an element by shifting elements and decrementing the size, and sorting an array using bubble sort by making multiple passes to swap adjacent elements in descending order.
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
The document discusses arrays and algorithms. It begins by defining arrays as lists of homogeneous data elements referenced by indices. Linear arrays are one-dimensional while multi-dimensional arrays include matrices. Common array operations like traversal, insertion, deletion and searching algorithms like linear and binary search are described. For linear search, the best, average and worst case complexities are analyzed to be O(1), O(n/2) and O(n) respectively. Binary search is more efficient, having a complexity of O(log n) as it halves the search space on each comparison. Examples and pseudocode are provided to illustrate key concepts.
The document discusses different data structures and sorting algorithms:
1. It describes arrays as a linear data structure where elements are stored in consecutive memory locations and can be accessed using an index.
2. It provides examples of traversing and manipulating arrays, such as inserting and deleting elements. Common sorting algorithms like bubble sort and selection sort are also summarized.
3. Bubble sort works by repeatedly stepping through the list and comparing adjacent elements, swapping them if they are in the wrong order until no swaps are needed, resulting in a sorted list in O(n^2) time. Selection sort finds the minimum element and swaps it into the front each iteration.
PPT Lecture 2.2.1 onn c++ data structuresmidtushar
This document provides information about an Elementary Data Structure Using C++ course. The course objectives are to enable students to understand C++ programming constructs, analyze and solve problems in C++, understand data structures concepts, and analyze algorithm efficiency. The course covers arrays, linear data structures, searching, sorting, and traversal algorithms. Key concepts discussed include linear arrays, multi-dimensional arrays, array implementation in memory, insertion and deletion in arrays, linear and binary search algorithms, and applications of arrays in sorting, matrix operations, and CPU scheduling.
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
1) The document discusses linear arrays and their representation in memory. A linear array stores elements in consecutive memory locations.
2) The number of elements in an array is given by the length formula: Length = Upper Bound - Lower Bound + 1. The address of each element can be calculated using the base address of the array.
3) Common array operations like traversing, inserting, and deleting elements are discussed along with algorithms to perform each operation. Representing polynomials and sparse matrices using arrays is also covered.
1. The document discusses various data structures concepts including arrays, dynamic arrays, operations on arrays like traversing, insertion, deletion, sorting, and searching.
2. It provides examples of declaring and initializing arrays, as well as dynamic array allocation using pointers and new/delete operators.
3. Searching techniques like linear search and binary search are explained, with linear search comparing each element sequentially while binary search eliminates half the elements at each step for sorted arrays.
The document summarizes various sorting algorithms:
- Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. It requires O(n^2) time.
- Insertion sort iterates through the list and inserts each element into its sorted position. It is an adaptive algorithm with O(n) time for nearly sorted inputs.
- Quicksort uses a divide and conquer approach, recursively partitioning the list around a pivot element and sorting the sublists. It has average case performance of O(nlogn) time.
This document contains information about arrays and algorithms for traversing, inserting, and deleting elements from a linear array. It defines an array as a list of homogeneous data elements referenced by consecutive integer indices and stored in successive memory locations. It provides pseudocode for algorithms to traverse a linear array by applying an operation to each element, insert a new element into a specified position by moving other elements, and delete an element from a specified position by moving elements into the freed space.
This document discusses linear arrays and algorithms for common operations on arrays such as traversing, inserting, deleting, and sorting elements. It defines a linear array as a list of homogeneous data elements with a lower and upper bound. Algorithms provided include traversing from the lower to upper bound, inserting an element at a specified position by shifting other elements and incrementing the size, deleting an element by shifting elements and decrementing the size, and sorting an array using bubble sort by making multiple passes to swap adjacent elements in descending order.
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
The document discusses arrays and algorithms. It begins by defining arrays as lists of homogeneous data elements referenced by indices. Linear arrays are one-dimensional while multi-dimensional arrays include matrices. Common array operations like traversal, insertion, deletion and searching algorithms like linear and binary search are described. For linear search, the best, average and worst case complexities are analyzed to be O(1), O(n/2) and O(n) respectively. Binary search is more efficient, having a complexity of O(log n) as it halves the search space on each comparison. Examples and pseudocode are provided to illustrate key concepts.
An array is a list of homogeneous (similar data type) elements stored in contiguous memory locations. Elements are accessed via an index. One dimensional arrays use a single subscript, while two dimensional arrays use two subscripts to reference an element. Arrays can be initialized during declaration. The size of an array is calculated as upper bound - lower bound + 1. Elements of arrays can be traversed, inserted, or deleted. Two dimensional arrays can be stored in row major or column major order, affecting the calculation of element addresses. Multidimensional arrays generalize this to any number of dimensions.
The document discusses various sorting algorithms and their complexity. It begins by defining sorting as arranging data in increasing or decreasing order. It then discusses the complexity of sorting algorithms in terms of comparisons, swaps, and assignments needed. Sorting algorithms are divided into internal sorts, which use only main memory, and external sorts, which use external storage like disks. Popular internal sorting algorithms discussed in detail include bubble sort, selection sort, insertion sort, and merge sort. Bubble sort has a time complexity of O(n2) while merge sort and quicksort have better time complexities of O(nlogn).
Quicksort is a divide and conquer algorithm that picks an element as a pivot and partitions the array around that pivot. It recursively sorts the sub-arrays on each side of the pivot. The algorithm involves picking a pivot element, partitioning the array by the pivot value, and then recursively sorting the sub-arrays. Quicksort has average case performance of O(n log n) time but can perform poorly on worst-case inputs.
This document provides information about sorting algorithms. It begins with an introduction to sorting, explaining why sorting is important and common sorting algorithms like merge sort, quicksort, heapsort, etc. It then discusses different types of sorting algorithms like comparison-based sorting and specialized sorting. The document proceeds to explain several specific sorting algorithms in detail, including bubble sort, selection sort, insertion sort, merge sort, and bitonic sort. It provides pseudocode for the algorithms and examples to illustrate how they work on sample data. The key details covered are the time complexity of common sorting algorithms and how different algorithms have tradeoffs in terms of speed and memory usage.
The document discusses sorting algorithms including bubble sort, selection sort, insertion sort, and merge sort. It provides pseudocode and explanations of how each algorithm works. Bubble sort, selection sort, and insertion sort have O(n2) runtime and are best for small datasets, while merge sort uses a divide-and-conquer approach to sort arrays with O(n log n) runtime, making it more efficient for large datasets. Radix sort is also discussed as an alternative sorting method that is optimized for certain data types.
The document discusses various sorting and searching algorithms. It begins by introducing selection sort, insertion sort, and bubble sort. It then covers merge sort and explains how it works by dividing the list, sorting sublists recursively, and merging the results. Finally, it discusses linear/sequential search and binary search, noting that sequential search checks every element while binary search repeatedly halves the search space.
The document discusses sorting algorithms, including insertion sort and bubble sort. It provides descriptions of how each algorithm works, including pseudocode. Key points made include:
- Insertion sort works by inserting elements into the sorted portion of the array, maintaining the invariant that the sorted portion remains sorted.
- Bubble sort works by "bubbling up" the largest elements to the end through successive passes over the array, swapping adjacent out-of-order elements.
- Optimizations for both algorithms include detecting if the array is already sorted to stop early.
This document provides an overview of several advanced sorting algorithms: Shell sort, Quick sort, Heap sort, and Merge sort. It describes the key ideas, time complexities, and provides examples of implementing each algorithm to sort sample data sets. Shell sort improves on insertion sort by sorting elements in a two-dimensional array. Quick sort uses a pivot element and partitions elements into left and right subsets. Heap sort uses a heap data structure and sorts by swapping elements. Merge sort divides the list recursively and then merges the sorted halves.
Selection sort works by iteratively finding the minimum element in the unsorted portion of an array and swapping it into the sorted position. It has a time complexity of O(n^2) and is inefficient for large data sets. Bubble sort compares adjacent elements and swaps them if out of order, causing the largest elements to "bubble" to the end with each pass. Insertion sort inserts elements into the sorted portion of the array by shifting greater elements to make room. Both bubble and insertion sort have quadratic time complexity but insertion sort is generally more efficient due to less element swapping.
Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This is repeated for each pair of adjacent elements with at least one swap happening per iteration until the list is fully sorted. Selection sort works by finding the minimum element in the unsorted section and swapping it with the leftmost element to build up the sorted section from left to right. Insertion sort maintains a sorted sub-list and inserts new elements into the correct position in the sub-list by shifting other elements over as needed.
The document describes three sorting algorithms: bubble sort, insertion sort, and selection sort. Bubble sort works by traversing a list from the beginning to end and swapping adjacent elements to bubble the largest values to the end over multiple passes until the list is fully sorted. Insertion sort iterates through a list and inserts each element into its sorted position by shifting other elements over. Selection sort finds the minimum element on each pass and swaps it into the front of the partially sorted list. All three algorithms have a worst-case quadratic runtime of O(n^2).
This document describes three sorting algorithms: bubble sort, insertion sort, and selection sort. Bubble sort works by repeatedly "bubbling up" the largest value to the end of the array through pairwise comparisons and swaps. Insertion sort inserts elements into the sorted portion of the array by comparing elements and swapping as needed. Selection sort finds the minimum element and swaps it into the front repeatedly. All three algorithms have a worst-case complexity of O(n2) due to the potential for many swaps or comparisons.
Arrays allow storing and manipulating a collection of related data elements. They can hold groups of integers, characters, or other data types. Declaring individual variables becomes difficult when storing many elements, so arrays provide an efficient solution. Arrays are declared with a datatype and size, and elements can be initialized, accessed, inserted, deleted, and manipulated using their index positions. Common array operations include displaying values, finding maximum/minimum values, calculating sums, and passing arrays to functions. Multi-dimensional arrays extend the concept to store elements in a table structure accessed by row and column indexes.
The document discusses arrays and their representation in memory. It contains 3 main points:
1) It introduces linear arrays and how they are represented in memory with sequential and contiguous locations. It also discusses multidimensional arrays.
2) It provides algorithms for common linear array operations like traversing, inserting, deleting and searching using binary search. It explains how each algorithm works through examples.
3) It discusses how 2D arrays are represented in memory and visualized, using an example of storing student exam marks in a 2D array.
Lecture 02: Preliminaries of Data structureNurjahan Nipa
This document provides an overview of algorithms and data structures. It begins with mathematical notation and complexity analysis. It then discusses control structures like sequence, selection, and iteration. Specific algorithms are described, including finding the largest element in an array, linear search, and the sieve method for finding prime numbers. Floor, ceiling, integer, absolute value, exponent, and logarithm functions are also covered. Overall, the document serves as a lecture outline on fundamental algorithmic concepts.
The greedy choice at each step is to select the talk that ends earliest among the compatible options. This maximizes the chance of fitting additional talks into the schedule.
Sorting arranges data in a specific order by comparing elements according to a key value. The main sorting methods are bubble sort, selection sort, insertion sort, quicksort, mergesort, heapsort, and radix sort. Hashing maps data to table indexes using a hash function to provide direct access, with the potential for collisions. Common hash functions include division, mid-square, and folding methods.
Sorting arranges data in a specific order by comparing elements according to a key value. The main sorting methods are bubble sort, selection sort, insertion sort, quicksort, mergesort, heapsort, and radix sort. Hashing maps data to table indexes using a hash function to provide direct access, with the potential for collisions. Common hash functions include division, mid-square, and folding methods.
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
An array is a list of homogeneous (similar data type) elements stored in contiguous memory locations. Elements are accessed via an index. One dimensional arrays use a single subscript, while two dimensional arrays use two subscripts to reference an element. Arrays can be initialized during declaration. The size of an array is calculated as upper bound - lower bound + 1. Elements of arrays can be traversed, inserted, or deleted. Two dimensional arrays can be stored in row major or column major order, affecting the calculation of element addresses. Multidimensional arrays generalize this to any number of dimensions.
The document discusses various sorting algorithms and their complexity. It begins by defining sorting as arranging data in increasing or decreasing order. It then discusses the complexity of sorting algorithms in terms of comparisons, swaps, and assignments needed. Sorting algorithms are divided into internal sorts, which use only main memory, and external sorts, which use external storage like disks. Popular internal sorting algorithms discussed in detail include bubble sort, selection sort, insertion sort, and merge sort. Bubble sort has a time complexity of O(n2) while merge sort and quicksort have better time complexities of O(nlogn).
Quicksort is a divide and conquer algorithm that picks an element as a pivot and partitions the array around that pivot. It recursively sorts the sub-arrays on each side of the pivot. The algorithm involves picking a pivot element, partitioning the array by the pivot value, and then recursively sorting the sub-arrays. Quicksort has average case performance of O(n log n) time but can perform poorly on worst-case inputs.
This document provides information about sorting algorithms. It begins with an introduction to sorting, explaining why sorting is important and common sorting algorithms like merge sort, quicksort, heapsort, etc. It then discusses different types of sorting algorithms like comparison-based sorting and specialized sorting. The document proceeds to explain several specific sorting algorithms in detail, including bubble sort, selection sort, insertion sort, merge sort, and bitonic sort. It provides pseudocode for the algorithms and examples to illustrate how they work on sample data. The key details covered are the time complexity of common sorting algorithms and how different algorithms have tradeoffs in terms of speed and memory usage.
The document discusses sorting algorithms including bubble sort, selection sort, insertion sort, and merge sort. It provides pseudocode and explanations of how each algorithm works. Bubble sort, selection sort, and insertion sort have O(n2) runtime and are best for small datasets, while merge sort uses a divide-and-conquer approach to sort arrays with O(n log n) runtime, making it more efficient for large datasets. Radix sort is also discussed as an alternative sorting method that is optimized for certain data types.
The document discusses various sorting and searching algorithms. It begins by introducing selection sort, insertion sort, and bubble sort. It then covers merge sort and explains how it works by dividing the list, sorting sublists recursively, and merging the results. Finally, it discusses linear/sequential search and binary search, noting that sequential search checks every element while binary search repeatedly halves the search space.
The document discusses sorting algorithms, including insertion sort and bubble sort. It provides descriptions of how each algorithm works, including pseudocode. Key points made include:
- Insertion sort works by inserting elements into the sorted portion of the array, maintaining the invariant that the sorted portion remains sorted.
- Bubble sort works by "bubbling up" the largest elements to the end through successive passes over the array, swapping adjacent out-of-order elements.
- Optimizations for both algorithms include detecting if the array is already sorted to stop early.
This document provides an overview of several advanced sorting algorithms: Shell sort, Quick sort, Heap sort, and Merge sort. It describes the key ideas, time complexities, and provides examples of implementing each algorithm to sort sample data sets. Shell sort improves on insertion sort by sorting elements in a two-dimensional array. Quick sort uses a pivot element and partitions elements into left and right subsets. Heap sort uses a heap data structure and sorts by swapping elements. Merge sort divides the list recursively and then merges the sorted halves.
Selection sort works by iteratively finding the minimum element in the unsorted portion of an array and swapping it into the sorted position. It has a time complexity of O(n^2) and is inefficient for large data sets. Bubble sort compares adjacent elements and swaps them if out of order, causing the largest elements to "bubble" to the end with each pass. Insertion sort inserts elements into the sorted portion of the array by shifting greater elements to make room. Both bubble and insertion sort have quadratic time complexity but insertion sort is generally more efficient due to less element swapping.
Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This is repeated for each pair of adjacent elements with at least one swap happening per iteration until the list is fully sorted. Selection sort works by finding the minimum element in the unsorted section and swapping it with the leftmost element to build up the sorted section from left to right. Insertion sort maintains a sorted sub-list and inserts new elements into the correct position in the sub-list by shifting other elements over as needed.
The document describes three sorting algorithms: bubble sort, insertion sort, and selection sort. Bubble sort works by traversing a list from the beginning to end and swapping adjacent elements to bubble the largest values to the end over multiple passes until the list is fully sorted. Insertion sort iterates through a list and inserts each element into its sorted position by shifting other elements over. Selection sort finds the minimum element on each pass and swaps it into the front of the partially sorted list. All three algorithms have a worst-case quadratic runtime of O(n^2).
This document describes three sorting algorithms: bubble sort, insertion sort, and selection sort. Bubble sort works by repeatedly "bubbling up" the largest value to the end of the array through pairwise comparisons and swaps. Insertion sort inserts elements into the sorted portion of the array by comparing elements and swapping as needed. Selection sort finds the minimum element and swaps it into the front repeatedly. All three algorithms have a worst-case complexity of O(n2) due to the potential for many swaps or comparisons.
Arrays allow storing and manipulating a collection of related data elements. They can hold groups of integers, characters, or other data types. Declaring individual variables becomes difficult when storing many elements, so arrays provide an efficient solution. Arrays are declared with a datatype and size, and elements can be initialized, accessed, inserted, deleted, and manipulated using their index positions. Common array operations include displaying values, finding maximum/minimum values, calculating sums, and passing arrays to functions. Multi-dimensional arrays extend the concept to store elements in a table structure accessed by row and column indexes.
The document discusses arrays and their representation in memory. It contains 3 main points:
1) It introduces linear arrays and how they are represented in memory with sequential and contiguous locations. It also discusses multidimensional arrays.
2) It provides algorithms for common linear array operations like traversing, inserting, deleting and searching using binary search. It explains how each algorithm works through examples.
3) It discusses how 2D arrays are represented in memory and visualized, using an example of storing student exam marks in a 2D array.
Lecture 02: Preliminaries of Data structureNurjahan Nipa
This document provides an overview of algorithms and data structures. It begins with mathematical notation and complexity analysis. It then discusses control structures like sequence, selection, and iteration. Specific algorithms are described, including finding the largest element in an array, linear search, and the sieve method for finding prime numbers. Floor, ceiling, integer, absolute value, exponent, and logarithm functions are also covered. Overall, the document serves as a lecture outline on fundamental algorithmic concepts.
The greedy choice at each step is to select the talk that ends earliest among the compatible options. This maximizes the chance of fitting additional talks into the schedule.
Sorting arranges data in a specific order by comparing elements according to a key value. The main sorting methods are bubble sort, selection sort, insertion sort, quicksort, mergesort, heapsort, and radix sort. Hashing maps data to table indexes using a hash function to provide direct access, with the potential for collisions. Common hash functions include division, mid-square, and folding methods.
Sorting arranges data in a specific order by comparing elements according to a key value. The main sorting methods are bubble sort, selection sort, insertion sort, quicksort, mergesort, heapsort, and radix sort. Hashing maps data to table indexes using a hash function to provide direct access, with the potential for collisions. Common hash functions include division, mid-square, and folding methods.
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
This presentation includes basic of PCOS their pathology and treatment and also Ayurveda correlation of PCOS and Ayurvedic line of treatment mentioned in classics.
Assessment and Planning in Educational technology.pptxKavitha Krishnan
In an education system, it is understood that assessment is only for the students, but on the other hand, the Assessment of teachers is also an important aspect of the education system that ensures teachers are providing high-quality instruction to students. The assessment process can be used to provide feedback and support for professional development, to inform decisions about teacher retention or promotion, or to evaluate teacher effectiveness for accountability purposes.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
हिंदी वर्णमाला पीपीटी, hindi alphabet PPT presentation, hindi varnamala PPT, Hindi Varnamala pdf, हिंदी स्वर, हिंदी व्यंजन, sikhiye hindi varnmala, dr. mulla adam ali, hindi language and literature, hindi alphabet with drawing, hindi alphabet pdf, hindi varnamala for childrens, hindi language, hindi varnamala practice for kids, https://www.drmullaadamali.com
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
2. Array
Data structures are classified as either linear or nonlinear.
A data structure is said to be linear if its elements form a sequence or a linear
list.
There are two basic ways of representing such linear structures in memory.
One way is to have the linear relationship between the elements represented
by means of sequential memory locations. These linear structures are called
arrays.
The other way is to have the linear relationship between the elements
represented by means of pointers or links. These linear structures are called
linked lists.
Nonlinear structures are trees and graphs.
3. Linear Arrays
A linear array is a list of finite number n of homogeneous data elements such that :
a) The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers.
b) The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called the length or size of the array.
Three numbers define an array : lower bound, upper bound, size.
a. The lower bound is the smallest subscript you can use in the array (usually 0)
b. The upper bound is the largest subscript you can use in the array
c. The size / length of the array refers to the number of elements in the array , It
can be computed as upper bound - lower bound + 1
Let, Array name is A then the elements of A is : a1,a2….. an
Or by the bracket notation A[1], A[2], A[3],…………., A[n]
The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
4. Linear Arrays
Example :
A six element linear array DATAconsisting of the integers.
247
56
429
135
87
156
DATA[1] = 247
DATA[2] = 56
DATA[3] = 429
DATA[4] = 135
DATA[5] = 87
DATA[6] = 156
1
2
3
4
5
6
DATA
5. Linear Arrays
Example :
An automobile company uses an array AUTO to record the number of auto mobile
sold each year from 1932 through 1984.
AUTO[k] = Number of auto mobiles sold in the year K
LB = 1932
UB = 1984
Length = UB – LB+1 = 1984 – 1930+1 =55
6. Representation of linear array in memory
Let LA be a linear array in the memory of the computer. The memory of the
computer is a sequence of addressed locations.
LA
1000
1001
1002
1003
1004
1005
Fig : Computer memory
The computer does not need to keep track of the
address of every element of LA, but needs to keep
track only of the first element of LA, denoted by
Base(LA)
Called the base address of LA. Using this address
Base(LA), the computer calculates the address of
any element of LA by the following formula :
LOC(LA[k]) = Base(LA) + w(K – lower bound)
Where w is the number of words per memory cell for
the array LA
7. Representation of linear array in memory
Example :
An automobile company uses an array AUTO to record
the number of auto mobile sold each year from 1932
through 1984. Suppose AUTO appears in memory as
pictured in fig A . That is Base(AUTO) = 200, and w =4
words per memory cell for AUTO. Then,
LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204
LOC(AUTO[1934]) = 208
the address of the array element for the year K = 1965
can be obtained by using :
LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower
bound)
=200+4(1965-1932)=332
200
201
202
203
204
205
206
207
208
209
210
211
212
Fig :A
AUTO[1932]
AUTO[1933]
AUTO[1934]
8. Traversing linear arrays
Print the contents of each element of DATA or Count the number of elements
of DATA with a given property. This can be accomplished by traversing DATA,
That is, by accessing and processing (visiting) each element of DATA exactly
once.
Algorithm 2.3: Given DATAis a linear array with lower bound LB and
upper bound UB . This algorithm traverses DATAapplying an operation
PROCESS to each element of DATA.
1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Apply PROCESS to DATA[k]
4. Set K : = K+1.
5. Exit.
9. Example :
An automobile company uses an array AUTO to record the number of auto
mobile sold each year from 1932 through 1984.
a)Find the number NUM of years during which more than 300 automobiles
were sold.
b) Print each year and the number of automobiles sold in that year
Traversing linear arrays
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : = NUM+1
3. Exit.
1.Repeat for K = 1932 to 1984:
Write : K,AUTO[K]
2. Exit.
10. Inserting and Deleting
Inserting refers to the operation of adding another element to the Array
Deleting refers to the operation of removing one element from the Array
Inserting an element somewhere in the middle of the array require that each
subsequent element be moved downward to new locations to accommodate the
new element and keep the order of the other elements.
Deleting an element somewhere in the middle of the array require that each
subsequent element be moved one location upward in order to “fill up” the
array. Fig shows Milon Inserted, Sumona deleted.
Dalia Rahaman
Sumona
Mubtasim Fuad
Anamul Haque
1
2
3
4
5
6
STUDENT
Dalia Rahaman
Sumona
Milon
Mubtasim Fuad
Anamul Haque
1
2
3
4
5
6
STUDENT
Dalia Rahaman
Milon
Mubtasim Fuad
Anamul Haque
1
2
3
4
5
6
STUDENT
11. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Here LA is linear array with N elements and K is a positive integer such that
K<=N.This algorithm inserts an element ITEM into the Kth position in LA.
ALGORITHM
Step 1.
Step 2.
Step 3.
Step 4.
[Initialize counter] Set J:=N
Repeat Steps 3 and 4] while J>=K
[Move Jth element downward] Set LA [J+1]: =LA[J]
[Decrease counter] Set J:=J-1
[End of step 2 loop]
Step 5
Step 6.
Step 7.
[Insert element] Set LA [K]: =ITEM
[Reset N] Set N:=N+1
Exit
Insertion
12. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)
ALGORITHM
Step 1.
Step 2.
Set ITEM: = LA[K]
Repeat for J=K to N-1
[Move J+1st element upward] Set LA [J]: =LA[J+1]
[End of loop]
Step 3
Step 4.
[Reset the number N of elements in LA] Set N:=N-1
Exit
Deletion
13.
14. Bubble sort
Bubble sort is one of the easiest sort algorithms. It is called bubble sort because
it will 'bubble' values in your list to the top.
Algorithm Bubble_Sort (DATA, N):
1. Repeat steps 2 and 3 for K = 1 to N-1.
2. Set PTR: =1.[Initializes pass pointer PTR]
3. Repeat while PTR<=N-K: [Executes pass]
a) If DATA[PTR]>DATA[PTR+1],then:
TEMP := A[PTR], A[PTR] := A[PTR+1], A[PTR+1] := temp
[End of if structure]
b) Set PTR: =PTR+1
[End of inner loop]
[End of step 1 Outer loop]
4. Exit
15. • Sorting takes an unordered collection and makes
it an ordered one.
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
Sorting : Bubble sort
16. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
77 42 35 12 101 5
1 2 3 4 5 6
17. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
5
12
35 101
1 2 3 4 5 6
77Swap
42
42 77
18. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
5
12
42 101
1 2 3 4 5 6
77 Swap
35
35 77
19. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
5
35
42 101
1 2 3 4 5 6
77Swap
12
12 77
20. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
42 35 12 77 101 5
1 2 3 4 5 6
No need to swap
21. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
77
12
35
42
1 2 3 4 5 6
101S
w
ap 5
5 101
22. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
42 35 12 77 5 101
1 2 3 4 5 6
Largest value correctly placed
24. Items of Interest
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• So we need to repeat this process
42 35 12 77 5 101
1 2 3 4 5 6
Largest value correctly placed
25. Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an element, we
place it in its correct location…
• Then we repeat the “bubble up” process N – 1
times.
• This guarantees we’ll correctly
place all N elements.
27. Summary
• “Bubble Up” algorithm will move largest value to
its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed:
– Maximum of N-1 times
– Can finish early if no swapping occurs
• We reduce the number of elements we compare
each time one is correctly placed
28. Complexity of the bubble sort algorithm
The time for a sorting algorithm is measured in terms of the number of
comparisons. The number f(n) of comparisons in the bubble sort is easily
computed. Specifically there are n -1 comparisons during first pass, which places
the largest element in the last position, there are n -2 comparisons in the second
step, which places the second largest element in the next – to - last position, and
so on. Thus
f(n) = (n-1)+(n-2)+. . . +2+1 =n(n-1)/2=n2/2+O(n)
In other words, The time required to execute bubble sort algorithm is proportional
to n2, where n is the number of input items.
Editor's Notes
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. In its most basic form, each node contains: data, and a reference (in other words, a link) to the next node in the sequence.