[SER222] Analytical Analysis (5/5): Input Dependence

Ruben Acuna
22 Sept 201712:52

Summary

TLDREn este video se discute cómo el carácter del input puede influir en la eficiencia de un método. Anteriormente se asumía que solo importaba el tamaño del input, pero el orden de los datos también afecta. Se usan ejemplos como la búsqueda lineal y el ordenamiento por selección e inserción, destacando que algunos algoritmos dependen del estado del input, como si está ordenado o no. También se menciona la dificultad de escribir una función de crecimiento precisa debido a esta dependencia, pero se puede realizar un análisis en términos de notación O grande.

Takeaways

  • 💡 La complejidad de un algoritmo puede depender de la forma en que se presente la entrada, no solo del tamaño de esta.
  • 📊 Las funciones de crecimiento normalmente solo consideran el tamaño de la entrada, pero este no es siempre el único factor.
  • 🔄 Un algoritmo de búsqueda lineal se comporta de manera diferente según cómo esté ordenada la entrada.
  • ⏳ El peor caso en la búsqueda lineal ocurre cuando el elemento buscado está al final de la lista, lo que requiere revisar todos los elementos.
  • ⚠️ No siempre es posible escribir una función de crecimiento precisa si la entrada afecta el comportamiento del algoritmo.
  • 🧠 Una alternativa es utilizar una aproximación, como una cota superior, para representar el peor caso de un algoritmo.
  • 🔀 La suposición de que los datos están aleatoriamente ordenados puede permitir una estimación promedio del rendimiento, por ejemplo, la búsqueda lineal puede completarse en la mitad de los elementos.
  • 📈 Los algoritmos de ordenación, como el de selección y el de inserción, pueden tener tiempos de ejecución diferentes según cómo esté ordenada la entrada.
  • ⚙️ La ordenación por selección siempre toma el mismo tiempo, independientemente del orden de la entrada, mientras que la ordenación por inserción es más eficiente cuando la entrada ya está ordenada.
  • ⏱️ Aunque la ordenación por inserción tiene un peor caso de O(n²), si la entrada está ordenada, puede comportarse en tiempo O(n), lo que la hace más eficiente en ciertos casos.

Q & A

  • ¿Por qué no es suficiente considerar solo el tamaño del input al analizar el rendimiento de un método?

    -Porque el rendimiento de un método puede depender no solo del tamaño del input, sino también de cómo están organizados los datos en el input. Por ejemplo, en una búsqueda lineal, el número de comparaciones puede variar significativamente si el input está ordenado o no.

  • ¿Qué problema plantea el suponer que todos los inputs del mismo tamaño son equivalentes?

    -Este supuesto ignora cómo está estructurado el input, lo que puede afectar significativamente el rendimiento. Dos inputs del mismo tamaño pueden tener diferentes configuraciones, lo que puede llevar a un número diferente de operaciones realizadas por un algoritmo.

  • ¿Cómo afecta la organización de los datos en el input a la búsqueda lineal?

    -En una búsqueda lineal, si el elemento buscado está al inicio del array, el algoritmo puede terminar rápidamente. Sin embargo, si el elemento está al final del array o no está presente, el algoritmo tendrá que recorrer todo el array, lo que aumenta el número de comparaciones.

  • ¿Es posible escribir una función de crecimiento exacta para la búsqueda lineal?

    -No, no es posible escribir una función de crecimiento exacta para la búsqueda lineal porque el número de comparaciones depende de cómo está organizado el input. Sin embargo, se puede usar una aproximación mediante notación O, donde el peor caso es O(n).

  • ¿Cómo podría ‘arreglarse’ el problema de escribir una función de crecimiento exacta?

    -Una forma de 'arreglar' este problema es hacer suposiciones adicionales sobre el input, como asumir que ya está ordenado. Bajo estas suposiciones, se puede escribir una función de crecimiento más precisa.

  • ¿Qué es el análisis amortizado en el contexto de la búsqueda lineal?

    -El análisis amortizado es una técnica que permite analizar el comportamiento promedio de un algoritmo. En el caso de la búsqueda lineal, se podría suponer que el input está ordenado aleatoriamente, lo que lleva a una función de crecimiento promedio de f(n) = 1/2 * n.

  • ¿Por qué no existe una función de crecimiento para algunas funciones, como el retorno en una búsqueda lineal?

    -Porque el número de comparaciones necesarias para determinar si un elemento está en la colección depende de la posición del elemento en el input. En algunos casos, el retorno será inmediato (1 comparación) y en otros, se necesitarán múltiples comparaciones.

  • ¿Cómo afecta la estructura del input a los algoritmos de ordenamiento como insertion sort y selection sort?

    -En el caso de insertion sort, si el input ya está ordenado, el algoritmo actuará de manera mucho más eficiente, en tiempo O(n). Sin embargo, en el caso de selection sort, el número de operaciones será constante sin importar cómo esté estructurado el input.

  • ¿Cuál es la diferencia clave entre insertion sort y selection sort en términos de dependencia del input?

    -Insertion sort depende de la organización del input. Si el input está desordenado, tendrá un comportamiento O(n^2), pero si está ordenado, tendrá un comportamiento O(n). Selection sort, por otro lado, siempre realizará la misma cantidad de operaciones independientemente del orden del input.

  • ¿Por qué es útil el análisis del peor caso en algoritmos como insertion sort?

    -El análisis del peor caso es útil porque proporciona un límite superior sobre la cantidad de tiempo que el algoritmo puede tardar. En el caso de insertion sort, aunque puede ser muy rápido en algunos casos, en el peor caso puede comportarse como O(n^2), lo cual es importante saber al analizar su eficiencia.

Outlines

00:00

🤔 Importancia del carácter del input en el rendimiento de un método

En este párrafo se discute la influencia del carácter del input en el rendimiento de un algoritmo, utilizando el tamaño del input como un ejemplo inicial. Se menciona que, hasta ahora, se ha asumido que el tamaño del input es el único factor importante, pero esto no es del todo correcto. Utilizando el ejemplo de una búsqueda lineal, se ilustra cómo el orden de los elementos dentro del input afecta directamente la cantidad de comparaciones necesarias, y por ende, el rendimiento del método. Se cuestiona la validez de una función de crecimiento simple cuando el rendimiento depende del carácter del input.

05:02

📉 Análisis del peor caso y complejidad Big-O

Este párrafo profundiza en el análisis del peor caso para un algoritmo, utilizando la búsqueda lineal como ejemplo. Se explica cómo, aunque no se puede escribir una función de crecimiento precisa debido a la dependencia del carácter del input, es posible realizar un análisis utilizando notación Big-O, como O(n) para el peor caso. También se menciona cómo hacer suposiciones adicionales, como el hecho de que el input esté ordenado aleatoriamente, permite escribir funciones aproximadas de crecimiento. Se introduce el concepto de análisis amortizado para casos en los que se asume un input aleatorio.

10:03

🔄 Dependencia del input en algoritmos de ordenación

Este párrafo introduce la idea de que los algoritmos de ordenación también tienen problemas de dependencia del input. Se utilizan ejemplos de algoritmos comunes como el ordenamiento por inserción y el ordenamiento por selección, los cuales solucionan el mismo problema pero con diferentes comportamientos dependiendo de cómo está estructurado el input. Mientras que el ordenamiento por selección toma siempre el mismo tiempo sin importar el estado del input, el ordenamiento por inserción varía, siendo mucho más rápido si el input ya está ordenado. Esto refleja cómo el carácter del input afecta el rendimiento de estos algoritmos.

Mindmap

Keywords

💡Tamaño del input

El 'tamaño del input' se refiere a la cantidad de datos que se alimentan en un algoritmo, por ejemplo, un arreglo de tamaño 5. En el video, el presentador menciona que en el pasado se asumía que el único factor importante era el tamaño del input. Sin embargo, argumenta que el comportamiento de algunos algoritmos depende no solo del tamaño, sino también de la disposición de los datos dentro del input.

💡Crecimiento de funciones

El 'crecimiento de funciones' se refiere a cómo aumenta el número de operaciones de un algoritmo a medida que crece el tamaño del input. El video menciona que las funciones de crecimiento como f(n) solo toman en cuenta el tamaño del input, lo cual puede ser una simplificación, ya que algunos algoritmos son sensibles a la disposición de los datos en el input.

💡Dependencia del input

La 'dependencia del input' se refiere a cómo el comportamiento de un algoritmo varía según la disposición de los datos en el input. El presentador utiliza el ejemplo de la búsqueda lineal, donde la cantidad de comparaciones depende de si el elemento buscado está al principio o al final del arreglo, lo que genera diferentes tiempos de ejecución.

💡Búsqueda lineal

La 'búsqueda lineal' es un algoritmo que busca un elemento en un arreglo recorriendo uno a uno sus elementos. El video menciona cómo su rendimiento puede variar dependiendo del orden de los elementos en el input. Por ejemplo, si el elemento a buscar está al principio, el algoritmo termina rápido, pero si está al final, necesita más comparaciones.

💡Big-O

'Big-O' es una notación utilizada para describir el comportamiento de la complejidad en el peor de los casos de un algoritmo. El presentador explica que cuando no es posible predecir con precisión la cantidad de operaciones basándose solo en el tamaño del input, la notación Big-O permite dar un límite superior de la cantidad de trabajo que el algoritmo hará, como en el caso del algoritmo de búsqueda lineal que tiene un peor caso de O(n).

💡Análisis amortizado

El 'análisis amortizado' es una técnica que se utiliza para analizar el rendimiento promedio de un algoritmo a lo largo del tiempo. En el video, se menciona brevemente este concepto para referirse a cómo, en promedio, se espera que un algoritmo recorra la mitad de un arreglo desordenado antes de encontrar el elemento buscado, lo que se representa como f(n) = 1/2 * n.

💡Ordenación por selección

La 'ordenación por selección' es un algoritmo de ordenación que selecciona repetidamente el elemento más pequeño de una lista no ordenada y lo coloca en la posición correcta. En el video, el presentador menciona que este algoritmo siempre ejecuta el mismo número de operaciones independientemente de cómo estén ordenados los datos inicialmente, lo que lo hace predecible en términos de complejidad.

💡Ordenación por inserción

La 'ordenación por inserción' es un algoritmo que ordena los elementos moviéndolos a su posición correcta en una lista ordenada parcial. A diferencia de la ordenación por selección, su comportamiento depende en gran medida de la disposición inicial de los datos. Si los datos ya están ordenados, el algoritmo actúa de manera más eficiente, mientras que si están desordenados, toma más tiempo.

💡Análisis del peor caso

El 'análisis del peor caso' es una técnica utilizada para evaluar el rendimiento de un algoritmo en las condiciones más desfavorables. En el video, el presentador destaca que el peor caso para la ordenación por inserción es cuando el input está completamente desordenado, lo que hace que el algoritmo se ejecute en O(n²).

💡Input aleatorio

El 'input aleatorio' hace referencia a una disposición de datos en el input que no sigue un patrón específico. En el video, el presentador menciona que asumir que el input está aleatoriamente mezclado es una forma común de simplificar el análisis de algoritmos, ya que elimina las preocupaciones sobre si el input está ordenado o desordenado de manera extrema.

Highlights

The input's structure, not just its size, significantly impacts the performance of algorithms.

The assumption that arrays of the same size behave similarly in terms of algorithm performance is flawed.

In linear search, input order affects performance; sorted inputs can be processed much faster.

The number of comparisons in linear search depends on the input structure, with sorted arrays requiring fewer comparisons.

It's challenging to define a precise growth function for an algorithm when its performance depends on input characteristics, not just size.

Big O notation provides a way to approximate performance, even when a precise growth function can't be determined.

The worst-case scenario for linear search leads to O(n) complexity, where n is the number of elements.

In some cases, an additional assumption, like assuming the input is sorted, can allow for a more accurate growth function.

Randomly shuffled input assumptions help create amortized analysis for performance estimation, predicting average cases.

The behavior of sorting algorithms like selection sort and insertion sort differs based on the input order.

Selection sort performs the same number of operations regardless of input order, making it consistent but not always efficient.

Insertion sort behaves differently depending on input order: in the best case (sorted input), it runs in O(n) time, while in the worst case (unsorted), it runs in O(n^2).

Worst-case analysis is crucial for understanding the upper limit of algorithm performance.

Input dependence is a common issue for many algorithms, affecting their time complexity.

When growth functions don't exist, worst-case and amortized analysis can provide a useful approximation for algorithm performance.

Transcripts

play00:04

- In this video, I wanna discuss the impact

play00:07

of the character of the input on how well a method runs.

play00:12

So in the past, basically we've been making this assumption

play00:14

that the only thing I really care about, in terms of input,

play00:16

is the size of it.

play00:17

So any array of size 10, you know, it'll look the same.

play00:20

And maybe you can get that, right, from the growth function.

play00:22

The growth functions are written something like f(n).

play00:24

There's only one thing there, right?

play00:25

There's only an n there.

play00:26

So if I have, you know, different arrays, I'll have,

play00:28

maybe, different stuff inside.

play00:29

They all, you know, if they're the same size,

play00:31

then, well, they all look the same to that growth function.

play00:33

All right, that's what we've been assuming so far.

play00:35

Let me actually draw a little bit here.

play00:37

So let's say, for example, we have N equal...

play00:41

five.

play00:43

So if we were to go over here and say something like

play00:46

A...

play00:47

B...

play00:49

C...

play00:51

D...

play00:53

E...

play00:56

That's an array of size five.

play00:57

I give it to a sorting program, right, it'll do its stuff.

play00:59

But I also have different inputs, right, of that same size.

play01:01

Maybe I just have the reverse order, E, D...

play01:06

C...

play01:07

B.

play01:08

And there's a little bit of an issue here, right,

play01:10

in that these things look different

play01:11

but they're the same size.

play01:12

Up until now, we've made the assumption that, oh,

play01:14

they're the same size, so they're equivalent.

play01:17

And that just really isn't the best assumption to make.

play01:21

So let's say that we have this linear search program.

play01:24

So linear search is a program, right, that basically looks

play01:27

at an input pool of elements, and it tells you is the sum

play01:31

in that collection or not, is it in that array or not.

play01:33

Well, if we think about maybe searching

play01:35

for something like...

play01:38

A, right?

play01:39

A, in the input, right, and the input

play01:41

can be the same size here, right?

play01:42

So I have here, right, two different inputs, okay?

play01:44

Say they're size five here, right?

play01:45

Feed them both to this program as the pool, right?

play01:50

Our target may be something like A.

play01:53

If I run this program on that first input,

play01:56

say this is input one, what happens is I finish immediately.

play02:01

Right, so because the data is set up a certain way,

play02:04

because it's sorted, right, A, B, C, D,

play02:07

if I run linear search on that and I'm looking for A,

play02:10

it's basically done, right?

play02:12

The first loop, right, looks to the first L,

play02:14

and the first L matches and returns true, done, right?

play02:16

Really, really easy, right?

play02:17

And very little combination to do.

play02:19

So maybe if we were saying here, you know,

play02:21

we're doing some analysis on this, and we wanna do analysis,

play02:23

you know, the right way and choose a good cause metric,

play02:25

maybe our cause metric could be something like the equality.

play02:28

Right, equalities are nice because there's something...

play02:29

Well, here it's nice

play02:30

because it happens inside a loop, right,

play02:31

something happens very frequently,

play02:33

and it's very kind of core to the idea of the algorithm,

play02:35

right, checking to see whether something's in a collection.

play02:36

So it'd be reasonable to count up

play02:37

the number of times that occurs.

play02:39

But if I have my input of size five that goes from A to E,

play02:44

what happened is this is only...

play02:47

gonna cause us to do one comparison,

play02:51

which is okay, it's nice.

play02:53

But what happens if I put the second input in there?

play02:55

If I put the second input in there, in the reverse order,

play02:58

E, D, C, B, A...

play03:00

well...

play03:01

I'm going to need more than one comparison.

play03:03

In fact, I need five comparisons because my linear search

play03:05

is gonna go and look at each of those elements in turn

play03:08

'til it gets to the very end.

play03:09

And so now I have this over here, this goes to five.

play03:15

So what is our result?

play03:18

Our result, right, well, conceptualize our result, here,

play03:22

is a linear search is dependent on its input size.

play03:25

Excuse me, a linear search is dependent on

play03:27

how its input looks.

play03:28

So if it looks one way, it'll work quickly.

play03:30

If it works another way, it'll, you know, not work quickly.

play03:33

But because it has that kind of character,

play03:36

it's no longer fair game for us

play03:37

to write an exact growth function, right?

play03:39

I can't have a function that, you know, I have...

play03:42

I can't have, you know, this, right?

play03:43

This is bad, right?

play03:45

F(5) equal one, f(5) equal...

play03:51

five.

play03:52

That's absolutely horrible, don't ever do that,

play03:55

that should make you very sad if you've paid attention

play03:57

in math classes, that's not a function.

play04:00

And again, right, the issue here, right, is that

play04:03

how the function works depends on how the input data looks.

play04:09

Generally what this implies

play04:11

is that we do not have a growth function, right?

play04:13

It may not be possible, actually, to write a growth function

play04:15

for a particular program if there's this dependency.

play04:20

And so, unfortunately, this happens with...

play04:22

Well, it's uncommon, right?

play04:24

So you'll see it although it's not, you know, super common.

play04:27

So there's a lot of things that a growth function

play04:29

does not exist for.

play04:30

And, in that case, what we're left with is using something

play04:33

that's an approximation, something like a bound.

play04:36

So, in this case, right, my...

play04:38

If I were to summarize my analysis here, I would say

play04:40

that my f(n)...

play04:44

We'll say that does not...

play04:46

exist.

play04:47

I'll actually come back to that 'cause there's a way

play04:48

we can force it to work, but the only thing

play04:50

we could really say is that this is maybe something

play04:52

like O(n), right, so this particular function here...

play04:59

Even though I can't tell exactly how many times

play05:01

I'll need you to make that comparison,

play05:03

what I can see is, oh, that comparison happens

play05:04

within a four-loop, that four-loop happens up to n times.

play05:07

Well, that's kinda like a worst case, right?

play05:09

N comparisons, so I go, hey, that's big O,

play05:11

that's the worst case right there, so this whole thing

play05:13

could be O(n).

play05:14

So this would be a fair analysis, that would be good to go.

play05:17

But again, the issue is that our straight-up growth function

play05:19

simply does not exist because the function basically

play05:22

isn't predictable enough, right?

play05:23

The amount of work it has to do isn't predictable

play05:25

from the input size alone or from the size array,

play05:27

and that is an issue.

play05:29

One thing we could do...

play05:33

maybe if we wanted to somehow "fix" this, is say,

play05:36

okay, I can make an additional assumption

play05:39

about how my input looks,

play05:40

and so I can say, okay, I can assume, for example,

play05:42

that it's already sorted.

play05:43

If it's already sorted, then, hey, my growth function

play05:46

is basically gonna be f(n) equal one or f(n) equal zero,

play05:50

depending on whether it's there or it's not there.

play05:52

If it's sorted in reverse order, right, I can make

play05:54

the assumption that it's gonna be f(n).

play05:55

F(n) equal n or something like that.

play05:57

Point is, if I make additional assumptions about my input,

play05:59

I can maybe then use those constraints to actually write

play06:03

a growth function for that.

play06:04

But I need additional assumptions.

play06:05

The size of the input is not alone, is not enough.

play06:08

One very common thing to do here is basically to assume

play06:11

that our input is randomly shuffled.

play06:15

If our input is randomly shuffled,

play06:17

we can actually say the following.

play06:19

F(n)...

play06:23

equal 1/2...

play06:26

n.

play06:27

And what that means, is it means we say, okay,

play06:31

on average, right, I will have seen my input

play06:35

by the time I get through half of the data, right?

play06:38

That's that one half doing there, right?

play06:39

We have to explore one half of those n elements

play06:43

before we're likely to see something.

play06:44

And once we see it, well, hey, you know, we can return true.

play06:48

So this is (murmurs), and it's called amortized analysis,

play06:51

and we're not gonna talk a lot about that in this class.

play06:54

The only time we'll use it is...

play06:57

The only time we'll use the idea is basically

play06:58

to say, okay, sometimes when you have a pool of elements,

play07:00

we just randomly assume that they're sorted.

play07:03

We'll assume that they're randomly ordered, right,

play07:04

because then we can, don't have to worry

play07:06

about these (murmurs) of, oh, it's done really quickly

play07:08

or it's done really slowly.

play07:10

If it's just all random, then we know there's

play07:12

for sure gonna be work to do.

play07:14

So just kinda keep that in the back of your head, right?

play07:15

That's another way to approach this,

play07:16

but we need additional assumptions.

play07:21

So, to answer that question there at the bottom, right,

play07:24

hopefully you got it, right?

play07:25

No, it's not possible to write a growth function for that.

play07:28

It's also not possible to write a growth function

play07:30

for something like the return.

play07:32

The return is something that, basically, I have to know,

play07:35

is the element in the collection or not, right?

play07:37

If I do, then the growth function for that is one.

play07:39

Otherwise, it's zero.

play07:40

And the opposite holds for the return false over here.

play07:49

Having said that, having worked out an analysis,

play07:51

let's actually look at sorting algorithms for a second

play07:53

because sorting algorithms have, basically,

play07:54

this same issue of input dependence.

play07:58

So hopefully in your previous courses, like CR200 or CSC205

play08:01

or CSD200 or many of the other classes that serve

play08:04

as prereqs for this, you've seen, you know, the algorithms

play08:08

for insertion sort and selection sort.

play08:10

So those are two algorithms that, basically, we give them

play08:12

some pool of elements, and at the end of the day,

play08:14

they produce for us those, basically, that same data

play08:17

but now in a sorted order.

play08:18

So maybe it's ascending, maybe it's descending.

play08:20

You know, it doesn't really matter.

play08:21

Point is, it's ordered according to some rule, right,

play08:23

less than or greater than.

play08:26

Okay, so think about those sorting algorithms

play08:29

for a second, right?

play08:30

Insertion sort, right, is about this idea

play08:31

of taking an element and basically shifting it

play08:32

into the place it belongs, whereas selection sort

play08:35

is about reaching into your collection of things to sort,

play08:39

taking out the smallest and putting it at the front,

play08:40

and then repeating it.

play08:41

So it's basically adjusting one element at a time

play08:43

versus insertion sort is about shifting over one element,

play08:45

which may kind of involve touching many other things

play08:47

along the way, and then getting it into its right position.

play08:50

So you have those two different algorithms,

play08:51

and these algorithms are basically an example, right,

play08:54

of things that solve the same problem,

play08:56

but one of them has a little bit of a different behavior

play08:58

depending on how the input looks.

play08:59

Now, if I have something like a selection sort,

play09:01

I can run it on...

play09:05

on any input of a particular size,

play09:07

and it'll all act the same.

play09:10

So if I have an input array of maybe, of size 10, right,

play09:12

it just doesn't matter what is in there.

play09:14

So it could be sorted already,

play09:15

it could be completely unsorted, it could be reverse order.

play09:17

No matter how it looks, selection sort will take

play09:20

the same amount of time, it'll always apply

play09:22

the same algorithmic kind of shuffling or de-shuffling

play09:25

to generate its output.

play09:27

So that's what I'm trying to basically indicate here

play09:29

in this graph.

play09:31

This graph on the right-hand side, what I have here is

play09:34

this axis over here is supposed to be number of operations.

play09:38

And, notice here, this is basically constant,

play09:40

so what I'm saying is, oh, hey, no matter what you give it,

play09:42

the same number of operations will be executed.

play09:45

And then over here, on this axis,

play09:46

which is a little bit tricky, this stuff over here,

play09:50

these are basically different inputs.

play09:51

So maybe, you know, this could go from ordered inputs

play09:55

over here to unordered.

play09:59

And even if something's really ordered, right,

play10:01

it still takes the same amount of time as something

play10:03

that's really unordered, right?

play10:04

That, how the input looks, just doesn't impact

play10:07

how selection sort acts.

play10:10

Okay, so that's pretty nice, and that's actually

play10:13

one of the advantages of selection sorts,

play10:14

they're very kind of reliable in terms of how much memory

play10:16

it uses or how much computation it uses.

play10:19

On the flip side, insertion sort, which also solves

play10:21

the same problem, does not have,

play10:24

kind of, that level of reliability.

play10:25

It's actually going to vary with what the input looks like.

play10:30

So worst case for insertion sort is looking at something

play10:33

that's completely unordered.

play10:34

So if something's unordered, insertion sort

play10:36

will look something like a couple of nested loops,

play10:40

it'll do something that's like O(n-squared).

play10:43

It's this line over here.

play10:46

So this is unordered.

play10:51

So that's okay, right?

play10:53

If we were to look at something like a selection sort,

play10:54

a selection sort is also O(n),

play10:55

so maybe there the behaviors are comparable.

play10:58

Now, what happens, though, is that in insertion sort,

play11:01

if I already have an input that's sorted,

play11:04

it acts completely different.

play11:05

It doesn't actually have nested loops anymore,

play11:07

it just has one loop, one loop that goes and looks

play11:09

at every element and checks to see whether that element

play11:11

is in its proper place.

play11:14

For an unordered case, right, its insertion sort

play11:16

will have two loops because it uses the inner loop

play11:18

to push the unordered elements into their proper position.

play11:22

But here, right, if we say, oh, hey, things are ordered,

play11:25

the inner loop basically never needs to run

play11:26

because that element doesn't need to be shifted

play11:28

into its proper position, so we're good to go.

play11:30

So it turns out that if I have things that look sorted,

play11:33

insertion sort acts much, much more

play11:35

like a linear time algorithm.

play11:37

So it looks like O(n) because somehow it turns

play11:39

from the sorting problem

play11:40

into the verification problem, right?

play11:42

Check to see whether this array is already sorted or not,

play11:44

that's what insertion sort is effectively doing

play11:46

on an input that's already sorted.

play11:48

So this is another example of input dependence.

play11:51

A lot of times, right, when I'm running code

play11:54

that has this dependency, and it's basically something

play11:56

for us to, well, we do need to worry about it.

play11:59

Usually what happens is...

play12:00

Well, what we hope happens is something like insertion sort,

play12:04

where we can find a good upper bound like O(n-squared)

play12:06

for it, and then in the back of our heads,

play12:09

we think to ourself, oh, cool, you know what?

play12:11

That algorithm, there are cases where it'll do better

play12:13

than that, it'll look more like O(n)

play12:14

or have a growth function that looks like n.

play12:18

That's nice, kinda, knowing your back of your head, right?

play12:20

Not just that it'll only take at most n-squared time,

play12:24

but that there is a good chance it'll take a lot less,

play12:27

so that's a very nice thing to know.

play12:29

Although, if we're writing this down, we do need

play12:30

to pay attention, right, to that worst case

play12:32

because the worst case generally how it gets us in trouble.

play12:36

All right, so that should give you guys a general feel

play12:38

for how, basically, methods may depend on their input.

play12:42

Try to keep in mind, right, that there's this dependency,

play12:44

and where the dependency exists, growth functions

play12:46

typically don't exist, but we still have a chance

play12:48

of doing some sort of big-O-style analysis instead.

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
AlgoritmosAnálisis Big OEficienciaBúsqueda linealOrdenaciónEstructura de datosDependencia de entradaCrecimiento de funcionesPeor casoProgramación