Bagaimana Anda menggeser data dalam array dengan python?

NumPy adalah bentuk singkatan dari Numerical Python. Ini digunakan untuk berbagai jenis operasi ilmiah dengan python. Numpy adalah perpustakaan besar dengan python yang digunakan untuk hampir semua jenis operasi ilmiah atau matematika. Itu sendiri merupakan array yang merupakan kumpulan dari berbagai metode dan fungsi untuk memproses array

NumPy Array - Menggeser elemen

Menggeser array termasuk menghapus elemen dari array dan mengembalikan elemen yang dihapus. Metode ini mengubah panjang array

Jika kita ingin menggeser elemen array NumPy, kita perlu menggunakan fungsi shift() dari perpustakaan scipy di mana defaultnya adalah membawa nilai konstanta dari luar array dengan nilai cval, atur di sini ke nan

Jumlah tempat di mana elemen digeser. Jika sebuah tupel, maka sumbu harus berupa tupel dengan ukuran yang sama, dan masing-masing sumbu yang diberikan digeser dengan angka yang sesuai. Jika sumbu int while adalah tupel int, maka nilai yang sama digunakan untuk semua sumbu yang diberikan

sumbu int atau tupel int, opsional

Sumbu atau sumbu di mana elemen digeser. Secara default, larik diratakan sebelum digeser, setelah itu bentuk aslinya dipulihkan

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
0
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0________1______4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
6
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
8________2______2________1______4

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
7
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_18_______7
3 4 5 6 7 1 2 
8

 

3 4 5 6 7 1 2 
_9

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

a)    Elements are first moved in first set – (See below diagram for this movement
1

 

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_22______3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
1
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
1
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
7

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_4

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_18_______0

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_18_______2

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_18_______4

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
1
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
1
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
7

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_4

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
03

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
05

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
1
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
1
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
1
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
7

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_18_______7
3 4 5 6 7 1 2 
8

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_4

3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
28
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30___________
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_6_______30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
___________________________________________________________________________

          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
1
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
43

[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
53
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
54

 

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_55

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
56
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
57
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
59

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
63
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
64

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_65
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
66

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
71
          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
1

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
63
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
64

 

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_78

Keluaran

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]

Kompleksitas Waktu. O(log10(Setengah tidak ada elemen yang ada dalam larik yang diberikan)).  

Ruang Bantu. O(1)

METODE 2 (Menggunakan array temp)

Tulis fungsi rotate(ar[], d, n) yang memutar arr[] dengan ukuran n kali d elemen.  

Bagaimana Anda menggeser data dalam array dengan python?

Rotasi array di atas sebesar 2 akan membuat array

Bagaimana Anda menggeser data dalam array dengan python?

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]

Python3




Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_79

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
81

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
83
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
85

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
1
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
92

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
94

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
1
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
07

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
09
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
11

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
25
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
27
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
83

________6

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_18_______7
3 4 5 6 7 1 2 
8

 

 

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_33

3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
28
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30___________
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_6_______30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________g EX

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
53
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
54
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
56
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
59
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
60
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
61
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
5
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
63

 

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_64

Keluaran

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]

Kompleksitas waktu. Pada)
Ruang Bantu. O(d)

METODE 3 (Putar satu per satu).  

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_

Untuk memutar satu per satu, simpan arr[0] dalam variabel sementara temp, pindahkan arr[1] ke arr[0], arr[2] ke arr[1] … dan akhirnya temp ke arr[n-1]
Mari kita ambil contoh yang sama arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Putar arr[] satu per 2 kali
Kita mendapatkan [2, 3, 4, 5, 6, 7, 1] setelah rotasi pertama dan [ 3, 4, 5, 6, 7, 1, 2] setelah rotasi kedua.  

Python3




Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_65

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
67

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
69
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
71
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
72
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
73

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
75

 

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
76

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
78

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
83
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
82
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
44

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
69
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
71
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
72
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
90
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
93

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
09
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
97
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
44

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
02
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
44
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
83

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_9

 

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_09

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
11

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
69
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
71
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
72
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
17

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
21
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
71
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
23________6______8
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
25
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

 

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_27

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_28

3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
28
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30___________
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_6_______30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________g EX

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
46
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
5
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
41
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
41
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

 

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_54

Keluaran

3 4 5 6 7 1 2 

Kompleksitas waktu. O(n * d)
Ruang Bantu. O(1)

METODE 4 (Algoritma Juggling)
Ini adalah perluasan dari metode 2. Alih-alih memindahkan satu per satu, bagilah array dalam set yang berbeda
di mana jumlah set sama dengan GCD dari n dan d dan pindahkan elemen di dalam set.  
Jika GCD adalah 1 seperti untuk contoh array di atas (n = 7 dan d =2), maka elemen akan dipindahkan dalam satu set saja, kita mulai saja dengan temp = arr[0] dan terus bergerak arr[I+d]
Berikut adalah contoh untuk n =12 dan d = 3. GCD adalah 3 dan

Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

a)    Elements are first moved in first set – (See below diagram for this movement

Bagaimana Anda menggeser data dalam array dengan python?

          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}

Python3




Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_55

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
67

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
69
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
71
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
72
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
63

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_9

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
66

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
83
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
09

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
72
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
1
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
66

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
79
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
80
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
72
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
4

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
79
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
56
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
87
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
89

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
90
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
80
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
80
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
0
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
95

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
79
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
56
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
80
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8________6______8
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
01

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
90
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
03

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
79
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
05
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
07

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
79
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
72
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
80

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
05
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
83

 

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_16

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_17

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
19

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
69
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
87
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
71
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
72
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
17

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
21
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
71
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
31________6______8
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
25
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_27

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_36

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
38

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
56
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
41
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
66

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
3 4 5 6 7 1 2 
7
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
48

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
4
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
65
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
66

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
9
3 4 5 6 7 1 2 
7
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
54
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
71
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
56

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_27

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_58

3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
28
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30___________
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_6_______30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________g EX

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
46
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
5
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
41
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
41
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

 

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
_54

Keluaran

3 4 5 6 7 1 2 

Kompleksitas waktu. Pada)
Ruang Bantu. O(1)

Pendekatan lain. Menggunakan Daftar mengiris

Python3




leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_85

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_86

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
2
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
88

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
89
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
25
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
92
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
2_______2
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
82
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
9
leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
96

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end
_89
3 4 5 6 7 1 2 
7
3 4 5 6 7 1 2 
8

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
_33

3 4 5 6 7 1 2 
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
8
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
28
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
5
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
___________________________________________________________________________________f

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
_51
3 4 5 6 7 1 2 
17

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
52
3 4 5 6 7 1 2 
20
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
57

Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
51
3 4 5 6 7 1 2 
23
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
5
Original array: [1, 2, 3, 4, 5, 6, 7, 8]
Rotated array:  [2, 3, 4, 5, 6, 7, 8, 1]
30
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
60
3 4 5 6 7 1 2 
27

 

3 4 5 6 7 1 2 
_28

Keluaran

[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]

Jika array perlu diputar lebih dari panjangnya maka mod harus dilakukan.  

Misalnya. putar arr[] berukuran n demi d di mana d lebih besar dari n. Dalam hal ini d%n harus dihitung dan diputar dengan hasil setelah mod.  

Bagaimana Anda menggeser nilai dalam array dengan Python?

Untuk menggeser bit elemen array dari array 2D ke kiri, gunakan numpy. left_shift() dengan Python Numpy. Bit digeser ke kiri dengan menambahkan x2 0s di sebelah kanan x1. Karena representasi internal angka dalam format biner, operasi ini setara dengan mengalikan x1 dengan 2**x2.

Bagaimana Anda menggeser elemen dalam array?

Cara memindahkan elemen larik dari satu posisi larik ke posisi larik lainnya. .
Buat variabel temp dan tetapkan nilai posisi aslinya
Sekarang, tetapkan nilai di posisi baru ke posisi semula
Terakhir, tetapkan nilai di temp ke posisi baru

Bagaimana Anda menggeser array ke kiri?

Array dapat dibiarkan berputar dengan menggeser elemen-elemennya ke posisi sebelumnya yang dapat dilakukan dengan memutar melalui array dan melakukan operasi arr[j] = arr[ . Elemen pertama dari array akan ditambahkan ke array terakhir yang diputar. . The first element of the array will be added to the last of rotated array.

Bagaimana Anda menggeser daftar dengan Python?

Memasukkan. n = 2, Daftar_1 = [1, 2, 3, 4, 5, 6]
Keluaran. Daftar_1 = [5, 6, 1, 2, 3, 4]
Penjelasan. Kami mendapatkan daftar keluaran setelah memutar kanan (searah jarum jam) daftar yang diberikan oleh 2