How to use insertion sort in an array with pascal?

by audrey.hodkiewicz , in category: Other , a year ago

How to use insertion sort in an array with pascal?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by scotty , a year ago

@audrey.hodkiewicz 

To implement insertion sort in Pascal, you can use the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
program InsertionSort;

const
  MAX_SIZE = 100;

type
  arr = array[1..MAX_SIZE] of integer;

var
  A: arr;
  n, i, j, temp: integer;

procedure InsertionSort(var A: arr; n: integer);
begin
  for i := 2 to n do
  begin
    temp := A[i];
    j := i - 1;

    while (j > 0) and (A[j] > temp) do
    begin
      A[j + 1] := A[j];
      j := j - 1;
    end;

    A[j + 1] := temp;
  end;
end;

begin
  writeln('Enter the number of elements in the array:');
  readln(n);

  writeln('Enter the elements:');
  for i := 1 to n do
  begin
    readln(A[i]);
  end;

  InsertionSort(A, n);

  writeln('Sorted array:');
  for i := 1 to n do
  begin
    writeln(A[i]);
  end;

end.


In this code, we have declared a constant MAX_SIZE which represents the maximum size of the array. We have also defined a type arr which is an array of integers.


The InsertionSort procedure takes the array A and its length n as parameters. It uses a loop to iterate over each element in the array and moves it to its correct position in the sorted portion of the array.


We have also included the main program which reads the input values from the user, calls the InsertionSort procedure to sort the array, and then prints the sorted array.


To run this program, you can compile and execute it using a Pascal compiler.

Member

by carlo , 4 months ago

@audrey.hodkiewicz 

To use the insertion sort algorithm in Pascal with an array, follow these steps:

  1. Declare a constant MAX_SIZE to represent the maximum size of the array. Set it to the desired value.
  2. Define a type arr which is an array of integers. The size of this array should match MAX_SIZE.
  3. Declare a variable A of type arr to store the input array.
  4. Declare variables n, i, j, and temp of type integer. n will be used to store the length of the array, i and j are loop variables, and temp is a temporary variable for swapping elements.
  5. Define a procedure InsertionSort which takes A and n as parameters.
  6. Inside the InsertionSort procedure, use a loop to iterate from the second element (i = 2) to the last element (i = n).
  7. Assign the value of A[i] to temp.
  8. Set j to i - 1.
  9. Use a while loop to compare temp with the elements in the sorted portion of the array (from A[j] to A[1]). If A[j] is greater than temp, move A[j] one position to the right (A[j + 1] := A[j]) and decrement j by 1.
  10. After the while loop, assign temp to A[j + 1].
  11. Outside the InsertionSort procedure, use the main program to read the number of elements in the array (n) and the elements themselves (A[i]). Then call the InsertionSort procedure passing A and n as parameters.
  12. Finally, in the main program, print the sorted array A.


Compile and run the program using a Pascal compiler to see the sorted array.


Note: This implementation assumes that the input array contains integers, but you can modify it to handle other data types as needed.