Efficient Algorithm Analysis" (28 characters)

Efficient Algorithm Analysis
Slide Note
Embed
Share

Algorithm analysis involves evaluating the efficiency of different algorithms by assessing their time and space requirements, focusing on estimating and reducing the time needed. Mathematical techniques are employed to compare the time efficiency of algorithms independently of specific implementations. Operations, loops, and subroutine calls impact the analysis, with simple operations taking one step, while complex operations vary. The execution time of algorithms is determined by the number of statements, if-else conditions, and costs associated with each operation.

  • Algorithm
  • Analysis
  • Efficiency
  • Time
  • Complexity (9
  • 8
  • 9
  • 4
  • 9 characters)

Uploaded on Apr 13, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Declaration of arrays type arrayName [ arraySize ]; Ex-double balance[10];

  2. Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write Ex-double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

  3. Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write Ex-double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

  4. Initializing Arrays You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array Ex-balance[4] = 50.0;

  5. Shown below is the pictorial representation of the array:

  6. double salary = balance[4];

  7. #include <stdio.h> int main () { int a[10],i,size; printf( \nhow many no of elements u want to scan ); scanf( %d ,&size); printf( \nEnter the elements in the array ); for(i=0;i<size;i++) { scanf( %d ,&a[i]); } //end for for(i=0;i<size;i++) { printf( The array is %d ,a[i]); //Displaying Array } //end for return 0; }

  8. 1 2 3 4 5

  9. type name[size1][size2]...[sizeN];

  10. multidimensional array is the two- dimensional array type arrayName [ x ][ y ];

  11. Initializing Two-Dimensional Arrays int a[3][4] = { {0, 1, 2, 3} , /* initializers for {4, 5, 6, 7} , {8, 9, 10, 11} /* initializers for row /* initializers for row indexed by 2 */ };

  12. Accessing Two-Dimensional Array Elements int val = a[2][3];

  13. For example, the following declaration creates a three dimensional integer array Ex-int threedim[5][10][4];

  14. Passing Arrays as Function Arguments in C void myFunction(int param[10]) { . . . //Statement Excution }

  15. Abstract Data Type ADT is useful tool for specifying the logical properties of a data type. A data type is a collection of values & the set of operations on the values. ADT refers to the mathematical concept that defines the data type. ADT is not concerned with implementation but is useful in making use of data type.

  16. ADT for an array Arrays are stored in consecutive set of memory locations. Array can be thought of as set of index and values. For each index which is defined there is a value associated with that index. There are two operations permitted on array data structure .retrieve and store

  17. ADT for an array CREATE()-produces empty array. RETRIVE(array,index)->value Takes as input array and index and either returns appropriate value or an error. STORE(array,index,value)-array used to enter new index value pairs.

  18. Introduction to arrays Representation and analysis Type variable_name[size] Operations with arrays: Copy Delete Insert Search Sort Merging of sorting arrays.

  19. Copy operation #include <stdio.h> int main() { int a[100],b[100] position, c n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &a[c]); printf("Enter %d elements\n", n); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", a[c]); //Coping the element of array a to b for( c = 0 ; c < n - 1 ; c++ ) { b[c]=a[c]; } } return 0; }

  20. Enter number of elements in array -4 Enter 4 elements 1 2 3 4 displaying array a 1 2 3 4 displaying array b 1 2 3 4

  21. Delete operation #include <stdio.h> int main() { int array[100], position, i, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( i = 0 ; i < n ; i++ ) scanf("%d", &array[i]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); for ( i = position ; i < n; i++ ) { array[i] = array[i+1]; } printf("Resultant array is\n"); for( i = 0 ; i < n-1 ; i++ ) printf("%d\n", array[i]); return 0; }

  22. Delete operation

  23. Inserting an element #include <stdio.h> int main() { int array[100], position, i, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (i= 0;i< n; i++) scanf("%d", &array[i]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (i = n - 1; i >= position ; i--) array[i+1] = array[i]; array[position] = value; printf("Resultant array is\n"); for (i= 0; i <= n; i++) printf("%d\n", array[i]); return 0; }

  24. Inserting an element

  25. Sort an array Int a[10]={5,4,3,2,1} for(i=0;i<n-1;i++) { for(j=0;j<=n-1;j++) { if(a[j]>a[j+1]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }

  26. Reverse array #include <stdio.h> int main() { int array[100], n, i, temp, end; scanf("%d", &n); end = n - 1; for (i = 0; i < n; i++) { scanf("%d", &array[i]); } for (i= 0; < n/2; i++) { t emp = array[i]; array[i] = array[end]; array[end] = temp; end--; } printf("Reversed array elements are:\n"); for ( i= 0; i < n; i++) { printf("%d\n", array[i]); } return 0; }

  27. Sort element using array int a[10]={5,4,3,2,1} for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }

  28. Merging of two arrays

  29. multidimensional array is the two- dimensional array type arrayName [ x ][ y ];

  30. m-no of rows n-no of columns Printf( \n Enter the rows and columns ); Scanf(%d %d ,&m,&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { Printf( \n Enter the value of(%d)(%d)= ,i,j); Scanf( %d ,&a[i][j]); }

  31. For(i=0;i<m;i++) { Printf( \n ); For(j=0;j<n;j++) { printf( %d ,&a[i][j]); } }

  32. int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; /* output each array element's value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %d\n", i,j, a[i][j] ); } } return 0; }

  33. Address Calculation in single (one) Dimension Array:

  34. Array of an element of an array say A[ I ] is calculated using the following formula: Address of A [ I ] = B + W * ( I LB ) Where, B = Base address W = Storage Size of one element stored in the array (in byte) I = Subscript of element whose address is to be found LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)

  35. Ex-Given the base address of an array B[1300 ..1900] as 1020 and size of each element is 2 bytes in the memory. Find the address of B[1700]. Solution: The given values are: B = 1020, LB = 1300, W = 2, I = 1700 Address of A [ I ] = B + W * ( I LB ) = 1020 + 2 * (1700 1300) = 1020 + 2 * 400 = 1020 + 800 = 1820 [Ans]

  36. While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations. Therefore, a 2-D array must be liberalized so as to enable their storage. There are two alternatives to achieve linearization: Row-Major and Column-Major.

  37. Address of an element of any array say A[ I ][ J ] is calculated in two forms as given: (1) Row Major System (2) Column Major System

  38. The address of a location in Row Major System is calculated using the following formula: Address of A [ I ][ J ] = B + W * [ N * ( I Lr ) + ( J Lc )

  39. The address of a location in Row Major System is calculated using the following formula: Address of A [ I ][ J ] = B + W * [ N * ( I Lr ) + ( J Lc ) B = Base address I = Row subscript of element whose address is to be found J = Column subscript of element whose address is to be found W = Storage Size of one element stored in the array (in byte) Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero) Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero) M = Number of row of the given matrix N = Number of column of the given matrix

  40. Column Major System: The address of a location in Column Major System is calculated using the following formula: Address of A [ I ][ J ] Column Major Wise = B + W * [( I Lr ) + M * ( J Lc )] B = Base address I = Row subscript of element whose address is to be found J = Column subscript of element whose address is to be found W = Storage Size of one element stored in the array (in byte) Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero) Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero) M = Number of row of the given matrix N = Number of column of the given matrix

  41. Important : Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] ) but if it is given as A[Lr- Ur, Lc- Uc]. In this case number of rows and columns are calculated using the following methods: Number of rows (M) will be calculated as = (Ur Lr) + 1 Number of columns (N) will be calculated as = (Uc Lc) + 1 And rest of the process will remain same as per requirement (Row Major Wise or Column Major Wise).

  42. Examples: Q 1. An array X [-15 .10, 15 40] requires one byte of storage. If beginning location is 1500 determine the location of X [15][20]. Solution: As you see here the number of rows and columns are not given in the question. So they are calculated as: Number or rows say M = (Ur Lr) + 1 = [10 (- 15)] +1 = 26 Number or columns say N = (Uc Lc) + 1 = [40 15)] +1 = 26

More Related Content