


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Some problems concerning arrays are more easily attacked when the array is first sorted. We are assuming that the array can be sorted by comparison, using standard algorithms, in time T(N) ( N log(N) ) Consider a problem related to arrays. Let the algorithm, as applied to sorted arrays, have time complexity TSP(N). Then the time complexity of the combined approach might be given as TP(N) = TSP(N) + ( N log(N) ) Remember that this equation is not to be taken literally. T(N) ( N log(N) ) does not imply that T(N) = N log(N)
The brute–force approach has time complexity T(N) ( N 2 ). This algorithm is best expressed using While loops. Algorithm Uniqueness ( A[1 .. N], N ) Unique = TRUE J = 1 DO XJ = A[J] K = J + 1 DO If (XJ = = A[K] ) Then Unique = FALSE Else K = K + 1 WHILE ( K N) AND Unique J = J + 1 WHILE ( J < N ) AND Unique Return Unique
The problem of uniqueness is one for which presorting clearly presents an advantage. There are other problems for which the applicability of presorting depends on the way in which the array search will be done. The standard approach to searching an unsorted array has T(N) ( N ). Binary search can search a sorted array with time complexity T(N) ( log(N) ). The combined presort / binary search approach thus has time complexity T(N) ( N log(N) + log(N) ), or T(N) ( N log(N) ). If one has a largely static array that is rarely changed but frequently searched, it might be worth the time to presort the array.